BookmarkSubscribeRSS Feed
Sean_OConnor
Fluorite | Level 6

Morning Folks,

 

I have the following type of date which has been created as a number;

 

8062023
12052023
26052023
26052023
6022020
10042023
3102017

 

What would be the best way to turn it into a SAS date variable?

 

Any help welcome.

 

Sean 

3 REPLIES 3
José_Costa_biw
Fluorite | Level 6

Hi Sean!

 

To convert your dates stored as numbers in the "format" DDMMYYYY (DMMYYYY if day is less than 10) to a SAS date, you need to:

1 - convert those numerics to characters, being careful to ensure that a leading 0 "appears" if the day is less than 10 - this can be done with a PUT function, using an adequate format;

2 - convert the resulting characters back to proper dates (stored as numeric) - this can be done with an INPUT function, using a proper informat.

 

Supposing that the column with the original dates is called numdate, and the new column with proper date will be called sasdate, then this is how I would do it:

 

sasdate = input(put(numdate,z8.),ddmmyy8.)

Of course, don't forget to apply a format (e.g. format sasdate ddmmyy10.; ) to the new column in order to make it readable by humans, if that's a requirement.

 

Hope this helps!

 

Best regards,

 

José Costa

john_mccall
SAS Employee
Hi. I just realized my answer is similar to Jose's excellent response.
Best.
john_mccall
SAS Employee

Hi.   Try the following.

 

Please let me know if this works for you.

Thanks,

John McCall (SAS Instructor)

 

/* Create a sample table*/
Data Example_Table;
Input Date;
datalines;
8062023
12052023
26052023
26052023
6022020
10042023
3102017
;
run;
/* Change numbers to SAS dates*/
Data Convert_to_SASDate;
set example_table(rename=(date=Original_Date));
Date=Input(put(original_Date,10.),ddmmyy10.);
format date mmddyy10.;
run;
proc print data=Convert_to_SASDate;
title "Does this conversion work for you?";
run;

 

 

 

Does this conversion work for you?

 

Obs Original_Date Date
1 8062023 06/08/2023
2 12052023 05/12/2023
3 26052023 05/26/2023
4 26052023 05/26/2023
5 6022020 02/06/2020
6 10042023 04/10/2023
7 3102017 10/03/2017

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!

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
  • 574 views
  • 1 like
  • 3 in conversation