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