BookmarkSubscribeRSS Feed
carmong
Obsidian | Level 7

I am attempting to change the appearance of Data values with date formats, but the dates are not converting property and giving me the wrong dates. 

I am trying to change DATETIME19. to a different format but the dates are coming out wrong (I would like to have it in the mmddyy format but tried other formats)

 

 

I have used the following code:

data deltbl; set deletion;
date2 = input(AdmitDate,anydtdte10.);
format date2 date9.;
datec = put(date2,date9.);
run;

 

I have also tried the following to change mmddyy.10 and it still isn't coming out right

data deltbl; set deletion;
date2 = input(AdmitDate,anydtdte10.);
format date2 mmddyy10.;
addate = put(date2,mmddyy10.);
hosp =catx("_", ref_HospitalCode, MRN, addate);
run;

sample date errors 2 jpeg.jpgsample date errors jpeg.jpg

 

4 REPLIES 4
Reeza
Super User

Your code is mostly correct except you have a date time, not a date. Use the Datepart() function on the variable to get only the date portion. 
Use ANYDTDTM to read in a date time instead of date. 


Here's a great, but longer and in depth, reference for dates and times in SAS
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...


@carmong wrote:

I am attempting to change the appearance of Data values with date formats, but the dates are not converting property and giving me the wrong dates. 

I am trying to change DATETIME19. to a different format but the dates are coming out wrong (I would like to have it in the mmddyy format but tried other formats)

 

 

I have used the following code:

data deltbl; set deletion;
date2 =datepart( input(AdmitDate,anydtdtm.));
format date2 date9.;
datec = put(date2,date9.);
run;

 

sample date errors 2 jpeg.jpgsample date errors jpeg.jpg

 


 

carmong
Obsidian | Level 7

Thank you for your response. Where would I put that in my code (and can you post an example)?

 

 

Tom
Super User Tom
Super User

The INPUT function is for converting character strings into values.  But your variable appears to be a DATETIME numeric variable, not a character string.  If you try to use a number as the first parameter to the INPUT() function SAS will have to first convert it to a character string.  It will use the BEST12. format to do that.

 

SAS stores DATETIME values as number of seconds.  Midnight on the 8th of May in 2020 is the number 1,904,515,200 which when printed using the BEST12. format will be the string "  1904515200".  Since your informat is only using the first 10 characters that will look like the string 19045152.  Which only makes sense as a date if you treat is as DDMMYYYY style.  Hence you have created the date of APR 19, 5152.

You just want to use the DATEPART() function to convert the number of seconds in your datetime variable to a number of days.  Then you can use the DATE format to display it.

data deltbl;
  set deletion;
  date2 = datepart(AdmitDate);
  format date2 date9.;
  datec = put(date2,date9.);
run;

 Or you could leave your variable as DATETIME and use the DTDATE format to display just the date part.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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
  • 4 replies
  • 1634 views
  • 0 likes
  • 3 in conversation