BookmarkSubscribeRSS Feed
MILKYLOVE
Calcite | Level 5

Hello

 

For some reasons I can't seem to do this. How do I convert a date

DATE = '20231002' to

DATE_EXTRACTION  = '02OCT2023' in SAS please? 

 

I have tried :

 

DATE_EXTRACTION = put(datepart(DATE), date9.);

 

But I get 22AUG1960 instead of 02OCT2023

 

 

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

Try this

 

data test;
   date = '20231002';
   new  = put(input(date, yymmdd8.), date9.);
run;
PaigeMiller
Diamond | Level 26

@MILKYLOVE wrote:

Hello

 

For some reasons I can't seem to do this. How do I convert a date

DATE = '20231002' to

DATE_EXTRACTION  = '02OCT2023' in SAS please? 

 

I have tried :

 

DATE_EXTRACTION = put(datepart(DATE), date9.);

 

But I get 22AUG1960 instead of 02OCT2023

 

 


Dates should never be character strings, they should be numbers. Numbers that represent the number of days since 01JAN1960.

 

Had you written this

 

DATE = '02OCT2023'd;
format date date9.;

 

you would have the correct date ... this is the way to translate dates that you understand to numerical SAS dates. Also please note that this will not work:

 

DATE = '20231002'd;

 

Lastly, when you have actual SAS dates, you do NOT need the DATEPART function; the only time you need the DATEPART function is when the value is SAS date/time values. But your value is a SAS date, not a date/time value, and DATEPART will not do meaningful things to date values.

 

 

--
Paige Miller

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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