Help using Base SAS procedures

intnx macro variable

Accepted Solution Solved
Reply
Super Contributor
Posts: 256
Accepted Solution

intnx macro variable


I'm getting an error when I run this code:

%let cutoff = 201506;

%let averageperiod = 8;

%let startqtr = %SYSFUNC(intnx('qtr', %input(%put(&cutoff*100+ 01,8.),yymmdd8.), -&averageperiod, 'same'));

%put &startqtr;

the error says...

ERROR: Open code statement recursion detected.

885  %let averageperiod = 8;

886  %let startqtr = %SYSFUNC(intnx('qtr', %input(%put(&cutoff*100+

886! 01,8.),yymmdd8.), -8, 'same'));

ERROR: Open code statement recursion detected.

887  %put &startqtr;

ERROR: Open code statement recursion detected.


Accepted Solutions
Solution
‎08-05-2015 06:45 AM
Super User
Super User
Posts: 7,392

Re: intnx macro variable

Why not simplify the code by using a call symput in a data _null_.

%let cutoff = 2015Q06;

%let averageperiod = 8;

data _null_;

     call symput('startqtr',intnx('qtr',input(&cutoff,yyq9.),-&averageperiod, 'same'));

run;


My personal preference is to not put actionable code in macro as it just gets too complicated and difficult to read:  Leave macros for parameters for the overall program, and pieces of code which you don't want to repeat.  Write base SAS to manipulate data.

View solution in original post


All Replies
Frequent Contributor
Posts: 144

Re: intnx macro variable

Hi,

the %put function exist? It seems that the error is because the %put, if you run

%let cutoff = 201506;

%let averageperiod = 8;

%let var=%put(&cutoff*100+ 01,8.);

You have the same error

Solution
‎08-05-2015 06:45 AM
Super User
Super User
Posts: 7,392

Re: intnx macro variable

Why not simplify the code by using a call symput in a data _null_.

%let cutoff = 2015Q06;

%let averageperiod = 8;

data _null_;

     call symput('startqtr',intnx('qtr',input(&cutoff,yyq9.),-&averageperiod, 'same'));

run;


My personal preference is to not put actionable code in macro as it just gets too complicated and difficult to read:  Leave macros for parameters for the overall program, and pieces of code which you don't want to repeat.  Write base SAS to manipulate data.

Super User
Posts: 6,928

Re: intnx macro variable

%put and %input are macro statements and not macro functions. %put (or %input) is not allowed within another macro statement.

See SAS(R) 9.2 Macro Language: Reference, paragraph "Solving Open Code Statement Recursion Problems".

I suggest you solve your conversion in a data _null_ step and use call symput to create the macro variable startqtr:

%let cutoff = 201506;

%let averageperiod = 8;

data _null_;

startqtr = intnx('qtr', input(put(&cutoff*100+ 01,8.),yymmdd8.), -&averageperiod, 'same');

call symput('startqtr',put(startqtr,best.));

run;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Respected Advisor
Posts: 3,777

Re: intnx macro variable

You have several things wrong starting the conversion of YYYYMM to date.  I formatted STARTQTR but you may want to leave it integer(sas date).  Consider the follow

/*
%let cutoff = 201506;
%let averageperiod = 8;
%let startqtr = %SYSFUNC(intnx('qtr', %input(%put(&cutoff*100+ 01,8.),yymmdd8.), -&averageperiod, 'same'));
%put &startqtr;
*/


%let cutoff = 201506;

/*To convert CUTOFF to SAS date you can use YYMMN informat*/
%let cutoffdate = %sysfunc(inputn(&cutoff,yymmn,6));
%put &=cutoffdate;

%let averageperiod = 8;
/*dont use quotes 'qtr' and 'same' are wrong*/
%let startqtr = %SYSFUNC(intnx(qtr, &cutoffdate, -&averageperiod, same),date9);
%put &=startqtr;

32        
/*To convert CUTOFF to SAS date you can use YYMMN informat*/
33        
%let cutoffdate = %sysfunc(inputn(&cutoff,yymmn,6));
34         %put &=cutoffdate;
CUTOFFDATE=20240
35        
36         %let averageperiod = 8;
37         /*dont use quotes 'qtr' and 'same' are wrong*/
38         %let startqtr = %SYSFUNC(intnx(qtr, &cutoffdate, -&averageperiod, same),date9);
39         %put &=startqtr;
STARTQTR=01JUN2013
☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 398 views
  • 1 like
  • 5 in conversation