SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
LL5
Pyrite | Level 9 LL5
Pyrite | Level 9

I have a sas dataset which needs to be loaded into MS Server Sql. The date fields I have in the SAS dataset are in mmddyy10. format (like 08/31/2020). However, those date columns are defined as datetime in the sql server table, all the date fields in the sql server table are shown as "2020-02-10 00:00:00.000".  As the result, after I load the data from SAS to sql server, all date field become "1960-01-01 06:08:50.000".

 

Is there a way to convert the date format to  yyyy-mm-dd hh:mm:ss.ss  in SAS before I load the data into SQL server? Or, is there a better alternative way to fix this problem (without altering the column type in the sql server table)?

Any advice would be highly appreciated. 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

In SAS a FORMAT is just instructions on how to display the values. 

 

If the values work correctly with the MMDDYY format then the values are number of days.  You need to convert the value to number of seconds.  

You can use the DHMS() function to convert from date values (# of days) to datetime values (# of seconds). You will need to pick a time of day, but people normally just use midnight and it looks like that is what you have stored in your external database. Make sure to attach a datetime type format so the values will display in a human recognizable way.

datetime_var = dhms(date_var,0,0,0);
format datetime_var datetime19.;

Note that which format you use to display the datetime values in SAS will not impact the value stored and does not need to match the way that your remote database displays that datetime value.

Note that since both date and datetime values are numbers you could actual re-use the same variable for the new values.

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

In SAS a FORMAT is just instructions on how to display the values. 

 

If the values work correctly with the MMDDYY format then the values are number of days.  You need to convert the value to number of seconds.  

You can use the DHMS() function to convert from date values (# of days) to datetime values (# of seconds). You will need to pick a time of day, but people normally just use midnight and it looks like that is what you have stored in your external database. Make sure to attach a datetime type format so the values will display in a human recognizable way.

datetime_var = dhms(date_var,0,0,0);
format datetime_var datetime19.;

Note that which format you use to display the datetime values in SAS will not impact the value stored and does not need to match the way that your remote database displays that datetime value.

Note that since both date and datetime values are numbers you could actual re-use the same variable for the new values.

LL5
Pyrite | Level 9 LL5
Pyrite | Level 9

Thanks Tom. I used this approach and now I am seeing "31AUG2020:00:00:00" for the datetime_var. I guess this will work when I loaded into sql server.

data example;
   characterdate = '2020-08-31';
   SasDate = input(characterdate,yymmdd10.);
   datetime_var = dhms(Sasdate,0,0,0);
   format datetime_var datetime20.;

run;
mkeintz
PROC Star

You need to change values.  The underlying date value is the number of days after (or before) 01jan1960.   Such values are appropriately formatted for display by such formats as  date9., yymmdd8., mmddyy8., etc.

 

DATETIME values are the number of seconds after or before  01jan1960:00:00:00.  These values are what is appropriate for datetime formats.  Applying datetime formats to numbers which are just number of days after 01jan1960, will assume you know what you are doing, and interpret the value as number of seconds after 01jan1960:00:00:00.

 

So first change the value.  Let's say your variable is named mydate.  Then you can issue

     olddate=mydate;   /*Just to save the old value*/
     format olddate date9. ;
     mydate=dhms(mydate,0,0,0);

Now the value in mydate conforms to the format you have applied to the corresponding field in your database.

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 3102 views
  • 4 likes
  • 3 in conversation