DATA Step, Macro, Functions and more

automation of calling a macro having year and quarter as parameter

Accepted Solution Solved
Reply
Contributor
Posts: 58
Accepted Solution

automation of calling a macro having year and quarter as parameter

[ Edited ]
I want to automate this code given below , I dont want write Year = 2014Q1 or QTR=14Q1.....
so once i enter to year 2018 , it should automatically create a call to year = 2018Q1 and QTR=18Q1

%MACRO MOBILEQ8 (YEAR, QTR); %MEND %MOBILEQ8 (YEAR = '2014Q1', QTR=14Q1); %MOBILEQ8 (YEAR = '2014Q2', QTR=14Q2); %MOBILEQ8 (YEAR = '2014Q3', QTR=14Q3); %MOBILEQ8 (YEAR = '2014Q4', QTR=14Q4); %MOBILEQ8 (YEAR = '2015Q1', QTR=15Q1); %MOBILEQ8 (YEAR = '2015Q2', QTR=15Q2); %MOBILEQ8 (YEAR = '2015Q3', QTR=15Q3); %MOBILEQ8 (YEAR = '2015Q4', QTR=15Q4); %MOBILEQ8 (YEAR = '2016Q1', QTR=16Q1); %MOBILEQ8 (YEAR = '2016Q2', QTR=16Q2); %MOBILEQ8 (YEAR = '2016Q3', QTR=16Q3); %MOBILEQ8 (YEAR = '2016Q4', QTR=16Q4); %MOBILEQ8 (YEAR = '2017Q1', QTR=17Q1); %MOBILEQ8 (YEAR = '2017Q2', QTR=17Q2); %MOBILEQ8 (YEAR = '2017Q3', QTR=17Q3); %MOBILEQ8 (YEAR = '2017Q4', QTR=17Q4);

Accepted Solutions
Solution
‎08-14-2017 12:32 PM
Super User
Posts: 6,522

Re: automation of calling a macro having year and quarter as parameter

Writing a new macro would loop through all the values.  It's somewhat easier if you can use double quotes instead of single quotes around the &YEAR values:

 

%macro loop;

   %local year quarter;

   %do year = 14 %to 17;

      %do quarter = 1 %to 4;

            %MOBILEQ8 (YEAR = "20&year.Q&quarter.", QTR = &year.Q&quarter)

      %end;

   %end;

%mend loop;

 

%loop

 

If you must have single quotes, the value for YEAR= becomes more complex:

 

YEAR = %unquote(%str(%'20&year.Q&quarter.%'))

 

The complication is due to the fact that single quotes suppress macro language activity (including resolution of macro variable references).

 

There are ways to make this more flexible (which years to process, which quarters to process), but this is a good starting point in any case.

View solution in original post


All Replies
Super User
Super User
Posts: 9,193

Re: automation of calling a macro having year and quarter as parameter

Ue the power of Base SAS to do data processing:

data _null_;
  do yr=2017 to 2017;
    do qtr=1 to 4;
      call execute('%mobileq8 (year='||put(year,4.)||'Q'||put(qtr,1.)||',qtr='||put(year,4.)||'Q'||put(qtr,1.)||');');
    end;
  end;
run;

Although why you are doing it this way in the first place is beyond me.  Put Year and QTR in your data as data items - whch they are - then use By Group Processing - a fundamental skill of SAS programming - which will make your code infinitely simpler, faster, and easier to maintain.

Solution
‎08-14-2017 12:32 PM
Super User
Posts: 6,522

Re: automation of calling a macro having year and quarter as parameter

Writing a new macro would loop through all the values.  It's somewhat easier if you can use double quotes instead of single quotes around the &YEAR values:

 

%macro loop;

   %local year quarter;

   %do year = 14 %to 17;

      %do quarter = 1 %to 4;

            %MOBILEQ8 (YEAR = "20&year.Q&quarter.", QTR = &year.Q&quarter)

      %end;

   %end;

%mend loop;

 

%loop

 

If you must have single quotes, the value for YEAR= becomes more complex:

 

YEAR = %unquote(%str(%'20&year.Q&quarter.%'))

 

The complication is due to the fact that single quotes suppress macro language activity (including resolution of macro variable references).

 

There are ways to make this more flexible (which years to process, which quarters to process), but this is a good starting point in any case.

Super User
Posts: 12,994

Re: automation of calling a macro having year and quarter as parameter

Style note:

Anyone giving me a YEAR variable that contains quarter information should expect a barrage of questions about the variable name and content. Similar for a quarter that contains year information.

 

And why is Year in quotes and quarter not? I find dealing with quotes as part of a variable generally a tad awkward to work with.

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 189 views
  • 4 likes
  • 4 in conversation