DATA Step, Macro, Functions and more

How to get month as a string from date in the format 'mm'

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

How to get month as a string from date in the format 'mm'

Hi All,

 

I'm relatively new to SAS programming having had experience with MSSQL, MySQL mostly.

 

I am trying to get the month as a string from a date in the format 'mm'.

For example: 01JAN2017 would result in '01'. 01FEB2017 would result in '02' etc.

I need the preceding 0 for months 1-9

 

I have been looking at PUT function(?) to do this but cannot find a format that is going to do what I need it to do.

 

Anyone have any solution or ideas to point me in the right direction?


Accepted Solutions
Solution
‎08-31-2017 12:41 AM
Super Contributor
Posts: 266

Re: How to get month as a string from date in the format 'mm'

Try this

 

data _null_;

dt='01Jan17'd;
mon_val=put(month(dt),z2.);
put mon_val=;
run;

View solution in original post


All Replies
Solution
‎08-31-2017 12:41 AM
Super Contributor
Posts: 266

Re: How to get month as a string from date in the format 'mm'

Try this

 

data _null_;

dt='01Jan17'd;
mon_val=put(month(dt),z2.);
put mon_val=;
run;
Occasional Contributor
Posts: 6

Re: How to get month as a string from date in the format 'mm'

You my friend, are a legend.
This appears to do exactly what I need!
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 86 views
  • 1 like
  • 2 in conversation