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![]()
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.