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

Hello. I am trying to convert a numeric date variable into a character. Code below. Please advise.

 

*previous step;
data have1; set have;
gap_months=birthdate-admdate;
format gap_months month.;
run;

*This provided me with the difference in months from the birth hospital visit to
the hospital readmission . Now I want to turn the variable gap_months into a character;


I tried the following, it didn't work - any suggestions? thank you;

data want; set have1;
 gap_monthschar=  put(input( gap_months,best.),z2.);
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

gap_months is, per definition from the previous step, numeric, so the conversion with the input() function is not necessary.

From your code I infer that birthdate and admdate are SAS dates, and to calculate the month difference between dates, one uses the intck() function:

gap_months = intck('month',birthdate,admdate,'c');

and then

gap_monthschar = put(gap_months,z2.);

both can be done in one step.

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

More than the code, a sample data of what you 'have' and what you 'want' will help many understand better plz

eabc0351
Quartz | Level 8

Yes, sorry about that. I figured it the syntax in the meantime. I was looking for the function month(). This helped me get the month of both dates in a format that I wanted. @novinosrin Thank you for your reply. 

Kurt_Bremser
Super User

gap_months is, per definition from the previous step, numeric, so the conversion with the input() function is not necessary.

From your code I infer that birthdate and admdate are SAS dates, and to calculate the month difference between dates, one uses the intck() function:

gap_months = intck('month',birthdate,admdate,'c');

and then

gap_monthschar = put(gap_months,z2.);

both can be done in one step.

eabc0351
Quartz | Level 8

Great @Kurt_Bremser . That solution worked, thank you.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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