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

I have a variable, dod, that contains the date of death as yearmo and does not contain days. How do I add the first of every month (01) to the end of every case in the dod column and then change the format to match year-mo-da?

 

Here is an example of the data:

 

dod

201705

201304

201709

201608

 

Once I add the days as 01 to every case, how do I chance to the below format?

 

dod_formatted

2017-05-01

2013-04-01

2017-09-01

2016-08-01

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Based on the error message you posted, I suspect you are wrong concerning one key item.  I suspect that YMDOD is actually a character variable that happens to contain digits, not a numeric variable.  A PROC CONTENTS would reveal that.  If that's the case, you need a simpler, slightly different solution:

 

data want;
   set have;
   ymdod_formatted = input(ymdod, yymmn6.);
   format ymdod_formatted yymmdd10.;
run;

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

Assuming that dod is a numeric variable

 

data have;
input dod;
datalines;
201705
201304
201709
201608
;

data want;
   set have;
   dod_formatted=input(put(dod, best6.), yymmn6.);
   format dod_formatted yymmdd10.;
run;
dwhitney
Calcite | Level 5

dod (should be "ymdod", sorry) is a numeric value. The data I have contains over 11 million cases. New to SAS coding. Is there a way to bypass the first set of code to avoid entering in the raw data using the "datalines" step?

 

Here is what I did with your code, but it is not working. The error message is below.

 

data want;
set have;

input ymdod;
ymdod_formatted=input(put(ymdod, best6.), yymmn6.);
format ymdod_formatted yymmdd10.;

run;

 

ERROR 48-59: The format $BEST was not found or could not be loaded.

anushreebiotech
Obsidian | Level 7

Hi @dwhitney ,

 

Please remove the input statement, since you are using the set statement.

 

Regards,

Anushree

PeterClemmensen
Tourmaline | Level 20

What is the name of your dataset? Then insert that instead of "have" in the Set Statement. Also, you don't need the Input Statement. I suspect, this will give you what you want

 

data want;
   set have;
   ymdod_formatted=input(put(ymdod, best6.), yymmn6.);
   format ymdod_formatted yymmdd10.;
run;
Astounding
PROC Star

Based on the error message you posted, I suspect you are wrong concerning one key item.  I suspect that YMDOD is actually a character variable that happens to contain digits, not a numeric variable.  A PROC CONTENTS would reveal that.  If that's the case, you need a simpler, slightly different solution:

 

data want;
   set have;
   ymdod_formatted = input(ymdod, yymmn6.);
   format ymdod_formatted yymmdd10.;
run;
dwhitney
Calcite | Level 5

You are correct! It was in the character format. Thanks so much! The code worked great!

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!

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
  • 6 replies
  • 1539 views
  • 0 likes
  • 4 in conversation