BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
dlavery1
Fluorite | Level 6

Hello, I have a program I'm running every month, so I've set up %let statements at the beginning of my program for whoever is running this to change. I'd like to construct other variables from these values, and so far I can do this for numeric variables fine, but I'm struggling with string variables.

 

Here's what I'm doing:

******************THESE LINES NEEDS TO BE CHANGED EVERY MONTH*****************;
%let year = 2023;
%let month = 08;
**********************************************************************************;

*---- this section constructs program-wide values from the month and year input above -------;

%let current_month = &year. *100 + &month.; *gets a numeric value in the 6-digit BLS format: e.g. 202002 for Feb 2020;
%let end_month = (&year. - 1) * 100 + (&month. - 1);

*%let length month_str $8.;
%let month_str = "";
if &month. = "01" then &month_str = "January";
else if &month. = "02" then &month_str = "February";
else if &month. = "03" then &month_str = "March";
else if &month. = "04" then &month_str = "April";
else if &month. = "05" then &month_str = "May";
else if &month. = "06" then &month_str = "June";
else if &month. = "07" then &month_str = "July";
else if &month. = "08" then &month_str = "August";
else if &month. = "09" then &month_str = "September";
else if &month. = "10" then &month_str = "October";
else if &month. = "11" then &month_str = "November";
else if &month. = "12" then &month_str = "December";

The month_str section is not working. I think it's due to the if statements being outside a data step.

 

All suggestions or insights are appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Are you asking how to convert a month number into a month name?

 

Might be easiest to convert it to a DATE first.

%let year = 2023;
%let month = 08;

%let month_string =%sysfunc(mdy(&month,1,&year),monname.);

%put &=year &=month &=month_string;

Result

4580   %put &=year &=month &=month_string;
YEAR=2023 MONTH=08 MONTH_STRING=August

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

Are you asking how to convert a month number into a month name?

 

Might be easiest to convert it to a DATE first.

%let year = 2023;
%let month = 08;

%let month_string =%sysfunc(mdy(&month,1,&year),monname.);

%put &=year &=month &=month_string;

Result

4580   %put &=year &=month &=month_string;
YEAR=2023 MONTH=08 MONTH_STRING=August
PaigeMiller
Diamond | Level 26

ADVICE: Do not work with dates and calendar time periods as character strings. This will always lead to inefficient programming. Work with dates and calendar items as valid SAS numeric date values, and then use built-in functions and formats and informats. Valid SAS numeric date values are the number of days since 01JAN1960.

 

 

/* Note: nothing needs to be changed each month */
%let today=%sysfunc(today());
%let current_month=%sysfunc(putn(&today,yymmn6.));
%let end_month=%sysfunc(intnx(month,&today,-12,b));
%let month_str=%sysfunc(putn(&today,monname12.));

 

Why don't your commands work for &month_str? Here's the first one

 

if &month. = "01" then &month_str = "January";

 

The IF command does not work outside of a DATA step. If you are doing this in a macro, you could use %IF, as shown below

 

%if &month. = "01" %then %let &month_str = "January";

 

This won't work either, because in the case of January you would have set &MONTH to have a value of 01, which is not equal to "01". Can you see why?

 

But anyway, all these IF statements are completely unnecessary if you have valid SAS date values, and you use the MONNAME format, as shown above.

--
Paige Miller

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 553 views
  • 2 likes
  • 3 in conversation