Desktop productivity for business analysts and programmers

char instead of date -why

Accepted Solution Solved
Reply
Contributor
Posts: 56
Accepted Solution

char instead of date -why

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?


Accepted Solutions
Solution
‎04-10-2012 11:10 AM
Super User
Super User
Posts: 6,363

Re: char instead of date -why

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


All Replies
Esteemed Advisor
Posts: 7,295

Re: char instead of date -why

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;

Solution
‎04-10-2012 11:10 AM
Super User
Super User
Posts: 6,363

Re: char instead of date -why

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 ;

Contributor
Posts: 56

Re: char instead of date -why

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

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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