BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
need_some_help
Calcite | Level 5

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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 ;

View solution in original post

3 REPLIES 3
art297
Opal | Level 21

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;

Tom
Super User Tom
Super User

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 ;

need_some_help
Calcite | Level 5

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 advicesSmiley Happy

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Creating Custom Steps in SAS Studio

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1504 views
  • 5 likes
  • 3 in conversation