BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
shami
Obsidian | Level 7

I have the following date variable:

Collection_date

1/1/2023 5:27:00 PM

2/2/2023 2:40:00 PM

3/4/2023 12:02:00 PM

4/6/2023 11:59:00 AM

 

And I would like to convert it or create a new date variable and have it in the following format:

Date

01Jan2023

02Feb2023
04Mar2023

 

Kindly help with the appropriate code for this.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Run PROC CONTENTS on your existing dataset. Is the current variable NUMERIC or CHARACTER?  Does it have a FORMAT attached to it?

 

If it is NUMERIC then you have a DATETIME value.  You could just change the format attached to the variable to have it print in that style.

format old dtdate9.;

Or you can use the DATEPART() function to convert it to a DATE value.  Then you could attach the DATE9. format to it so it will print in the style you show.

new=datepart(old);
format new date9.;

Since both DATETIME and DATE values are stored as NUMERIC variables you could replace the values of the original variable instead.

old=datepart(old);
format old date9.;

 

If it is CHARACTER then you can use the INPUT() function with the MMDDYY informat to convert it to a DATE value.  Note that your strings seem to using inconsistent length for the date part.  So you will want to use SCAN() to pull out just the first word from the string.

new=input(scan(old,1,' '),mmddyy10.);
format new date9.;

 Since DATE values are numeric you will need to make a NEW variable.  If you wanted to store it back into the old variable you would need to add a PUT() function call to generate a character string.

old=put(input(scan(old,1,' '),mmddyy10.),date9.);

 

View solution in original post

1 REPLY 1
Tom
Super User Tom
Super User

Run PROC CONTENTS on your existing dataset. Is the current variable NUMERIC or CHARACTER?  Does it have a FORMAT attached to it?

 

If it is NUMERIC then you have a DATETIME value.  You could just change the format attached to the variable to have it print in that style.

format old dtdate9.;

Or you can use the DATEPART() function to convert it to a DATE value.  Then you could attach the DATE9. format to it so it will print in the style you show.

new=datepart(old);
format new date9.;

Since both DATETIME and DATE values are stored as NUMERIC variables you could replace the values of the original variable instead.

old=datepart(old);
format old date9.;

 

If it is CHARACTER then you can use the INPUT() function with the MMDDYY informat to convert it to a DATE value.  Note that your strings seem to using inconsistent length for the date part.  So you will want to use SCAN() to pull out just the first word from the string.

new=input(scan(old,1,' '),mmddyy10.);
format new date9.;

 Since DATE values are numeric you will need to make a NEW variable.  If you wanted to store it back into the old variable you would need to add a PUT() function call to generate a character string.

old=put(input(scan(old,1,' '),mmddyy10.),date9.);

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 3240 views
  • 1 like
  • 2 in conversation