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

Hi, 

 

I am trying to figure out how to format a character variable of a date that appears in the character format "8-Jun-2020" to a numeric mmddyy10 variable: "06/08/2020". I tried to use "substr" and "put", but the issue is that some dates appear as "22-Jun-2020", so the day part of the variable takes up 2 spaces instead of one (22 vs. 8).  

 

Things I've tried: 

data import;
set import;
birth_date_new = catx("-", put(birth_date, mmddyy10.)); 
run; 

AND 

data import; 
set import; 
birth_date_new= catx("-", substr(birth_date, 3,3),substr(birth_date, 1,1),substr(birth_date, 7,4)) OR
birth_date_new = catx("-", substr(birth_date,3,3), substr(birth_date,1,2), substr(birth_date,8,4));
format birth_date mmddyyyy10.; 
run;

Let me know if you can help, thanks! 

Clare 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Don't do this:

data import;
set import;

If something untoward happens in the data step, the dataset is destroyed and you need to re-create it. Sometimes this might mean you have to look for a backup.

Do this to convert your date value:

birth_date_new = input(birth_date,date11.); 
format birth_date_new mmddyy10.;

View solution in original post

3 REPLIES 3
SASKiwi
PROC Star

If your date variable is indeed character to start with this should work:

data test;
  date = '01-12-2020';
  date_new = input(date, ddmmyy10.);
  format date_new mmddyyd10.;
  put _all_;
run;
Kurt_Bremser
Super User

Don't do this:

data import;
set import;

If something untoward happens in the data step, the dataset is destroyed and you need to re-create it. Sometimes this might mean you have to look for a backup.

Do this to convert your date value:

birth_date_new = input(birth_date,date11.); 
format birth_date_new mmddyy10.;
claremc
Obsidian | Level 7

That worked! Thank you so much! 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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