SAS Enterprise Guide

Desktop productivity for business analysts and programmers
BookmarkSubscribeRSS Feed
Sean_OConnor
Obsidian | Level 7

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-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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