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! 

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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