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

sas-innovate-2024.png

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.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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