Hi, everyone!
Recently I had to work with unix-timestamp data and found out one interesting thing:
proc sql;
create table work.why as
select put(dhms('01jan1970'd,4,0,1310018872/*unixtimestamp*/),datetime18.) as char_not_date /*<-- !!!*/
from sashelp.macrs7 where year=8 /*~dummy table*/;
quit;
'put' in a date-format gives me char... Why is that? What do I miss?
PUT function always returns a string. That is what it is for. To display data in a given format.
If you want a datetime variable then remove the PUT() function and instead attach the format to the variable.
select dhms('01jan1970'd,4,0,1310018872) as datetime format=datetime18.
Why did you set the Hour to 4 AM?
You could also just add the seconds between 01JAN1970 and 01JAN1960.
sasdt = unixdt - '01jan1960:00:00'dt + '01jan1970:00:00'dt ;
Because that is what the put function does (see: http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000199354.htm )
Did you want it to be a datetime? If so, you could use:
proc sql;
create table work.why as
select input(put(dhms('01jan1970'd,4,0,1310018872/*unixtimestamp*/),datetime18.),datetime24.) as char_not_date format=datetime24. /*<-- !!!*/
from sashelp.macrs7 where year=8 /*~dummy table*/;
quit;
PUT function always returns a string. That is what it is for. To display data in a given format.
If you want a datetime variable then remove the PUT() function and instead attach the format to the variable.
select dhms('01jan1970'd,4,0,1310018872) as datetime format=datetime18.
Why did you set the Hour to 4 AM?
You could also just add the seconds between 01JAN1970 and 01JAN1960.
sasdt = unixdt - '01jan1960:00:00'dt + '01jan1970:00:00'dt ;
I misunderstood put-function principle, I thought it converts type of value (in documentation it is said: 'the PUT function returns a numeric value as a character string', so I decided it acts like converter...). Now I would use an explicit way of assigning format like you offered - there seems to be less points which can be mistaken) Thanks a lot for both advices
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.