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);
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.
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.
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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.