BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
subrat1
Fluorite | Level 6
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);
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Astounding
PROC Star

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.

ballardw
Super User

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 3 replies
  • 1130 views
  • 4 likes
  • 4 in conversation