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!
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
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
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.
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
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.
Ready to level-up your skills? Choose your own adventure.