BookmarkSubscribeRSS Feed
BCNAV
Quartz | Level 8

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

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

BCNAV
Quartz | Level 8

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.

Kurt_Bremser
Super User

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 

malmario
Calcite | Level 5

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;
Tom
Super User Tom
Super User

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
johnsville
Obsidian | Level 7

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 ;
Ksharp
Super User

%let cmonth = %sysfunc(  month( "&sysdate"d ) ,monname8. ) ;

%put &cmonth;


sas-innovate-2024.png

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.

 

Register now!

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
  • 7 replies
  • 6172 views
  • 1 like
  • 7 in conversation