I have the following, which is used to setup variables throughout a project:
%macro dateset;
%global cmonth pmonth cyear pyear cmonth_name;
%let sysmonth = %sysfunc(month("&sysdate"d));
%let sysyear = %sysfunc(year("&sysdate"d));
%if &sysmonth = 1 %then %do;
%let cmonth = 12;
%let cmonth_name = "December";
%let pmonth = 11;
%let cyear = %sysevalf(&sysyear - 1);
%let pyear = &cyear;
%end;
%if &sysmonth = 2 %then %do;
%let cmonth = 1;
%let cmonth_name = "January";
%let pmonth = 12;
%let cyear = &sysyear;
%let pyear = %sysevalf(&cyear - 1);
%end;
%if &sysmonth >= 3 %then %do;
%let cmonth = %sysevalf(&sysmonth - 1);
%let cmonth_name = ?????
%let pmonth = %sysevalf(&sysmonth - 2);
%let cyear = &sysyear;
%let pyear = &cyear;
%end;
%mend;
%dateset;
%put &cmonth &cyear &cmonth_name &pmonth &pyear;
I am stuck with converting the month number in:
%let cmonth = %sysevalf(&sysmonth - 1);
%let cmonth_name = ?????
Probably easy for some here....but I am stuck. Thanks
Well, lets start with the usual. Its not a good idea to create global macro variables inside code like that. It can lead to tricky bugs.
Second, why do you need these macro variables? As you provide there, all of them are available from one - sysdate - so why bother creating all of those macro variables?
Third, Base SAS is the programming language, use it always. It will cover every situation, macro is just additional find/replace system.
To illustrate the Base SAS route, and how much simpler it is, consider:
data want; original_month=put(month("&sysdate."d),monname8.); original_year=year("&sysdate."d); run;
Simple code, to do simple tasks.
These variables are used for datasteps, sql, and excel output creation. It is often much easier to have these setup so that other users can see where they come from once. I know there are neater ways of doing things, but this is what has been asked of me for now.
That is so easy to do in a data step that just looking at this macro code abomination makes my stomach hurt:
data _null_;
call symputx('cmonth',month(today()));
call symputx('cmonth_name',put(today(),monname.));
call symputx('pmonth',month(intnx('month',today(),-1)));
call symputx('cyear',year(today()));
call symputx('pyear',year(intnx('month',today(),-1)));
run;
Bottom line: do not abuse the macro preprocessor for handling data.
Edit: removed the custom format, as monname. can do it. And removed the unnecessary MONTH() function, as pointed out by @malmario
FYI - This code returns the wrong value for cmonth_name. Don't use the month() function when using monname. The format needs to be used with a date.
I modified here for the correct solution for the request. Notice the difference in line 3:
data _null_;
call symputx('cmonth',month(today()));
call symputx('cmonth_name',put(today(),monname.));
call symputx('pmonth',month(intnx('month',today(),-1)));
call symputx('cyear',year(today()));
call symputx('pyear',year(intnx('month',today(),-1)));
run;
Use the MONNAME format. For that you need an actual date value, not a month number.
data _null_;
now = today();
current_month =intnx('month',now,-1,'b');
previous_month = intnx('month',now,-2,'b');
call symputx('cmonth',month(current_month),'g');
call symputx('pmonth',month(previous_month),'g');
call symputx('cyear',year(current_month),'g');
call symputx('pyear',year(previous_month),'g');
call symputx('cmonth_name',put(current_month,monname.),'g');
call symputx('pmonth_name',put(previous_month,monname.),'g');
run;
%put &=cmonth &=cyear &=cmonth_name ;
%put &=pmonth &=pyear &=pmonth_name ;
12 %put &=cmonth &=cyear &=cmonth_name ; CMONTH=11 CYEAR=2018 CMONTH_NAME=November 13 %put &=pmonth &=pyear &=pmonth_name ; PMONTH=10 PYEAR=2018 PMONTH_NAME=October
the macro can perform data step functions if you wish:
%let cmonth = %sysfunc( month( "&sysdate"d ) ) ;
%let cmonth_name = %sysfunc( putn( "&sysdate"d , monname12. ) ) ;
%put *** &=cmonth ;
%put *** &=cmonth_name ;
%let pmonth = %sysfunc( month ( %sysfunc( intnx (month , "&sysdate"d , -1 ) ) ) ) ;
%let pmonth_name = %sysfunc( putn( %sysfunc(intnx( month , "&sysdate"d , -1 ) ) , monname12 ) ) ;
%put *** &=pmonth ;
%put *** &=pmonth_name ;
%let cmonth = %sysfunc( month( "&sysdate"d ) ,monname8. ) ; %put &cmonth;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.