ibm-cloudspss-modeler

How to split timestamp field into Year, Month and Day, etc?


I have a timestamp field which has this definition:

Time interval: the beginning of the time interval expressed as the number of millisecond elapsed from the Unix Epoch on January 1st, 1970 at UTC. The end of the time interval can be obtained by adding 600000 milliseconds (10 minutes) to this value. TYPE: numeric

I would like to split this field into Year, Month, Day of Month, Day of Week, Week Number.

It appears that I would need to use a Derive field with a Formula. But as a user new to the SPSS world, it isn't clear to me how I would use the derive field to do this.

The equivalent in pandas is:

df['Datetime'] = pd.to_datetime(df['Time interval'].astype(int))

df['Year'] = df['Datetime'].dt.year
df['Month'] = df['Datetime'].dt.month
df['Day'] = df['Datetime'].dt.day
df['DayOfWeek'] = df['Datetime'].dt.dayofweek

Solution

  • Do you want to create the 5 variables in separate, right?

    For create:

    **1) Year - Use a derive node and call the new variable as 'Year' with the syntax: "datetime_year(field)" -> will extract the year in numbers (2012)

    2) Month- Use a derive node and call the new variable as 'Month' with the syntax: "datetime_month(field)" -> will extract the month in numbers (1 to 12)

    3) Day of Month- Use a derive node and call the new variable as 'DayMonth' with the syntax: "datetime_day(field)" -> will extract the date of the month in numbers (1 to 31)

    4) Day of Week - Use a derive node and call the new variable as 'DayWeek' with the syntax: "datetime_weekday(field)" -> will extract the weekday in numbers (1 to 7)

    5) Week Number - Use a derive node and call the new variable as 'WeekNumb' with the syntax: "date_iso_week(field)" -> ISO 8601 (it's the only function that I never used in your list).**

    Also, you can check others expressions inside the derive node tab, just select all functions and make some tests.

    IBM Ref

    I hope to have been helpful.