BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ronein
Onyx | Level 15

Hello

There is a process that perform summary statistics from multiple data sets.

Each data set contain the name with YYMM format.

for example:

t2001 includes data for JAN 2020

t2004 includes data for APR 2020

and so on

 

I know how to run the macro manually for each month that I need.

If the name of the macro is RRR and I want to run for periods 2001 2004 2005 2006  then  I need to run the statements:

%RRR(2001);

%RRR(2004);

%RRR(2005);

%RRR(2006);

 

My question is what is the way to define one macro parameter with %let that includes the periods that I want to run for them the macro and then to run the macro for each of the arguments in the macro parameter

%let vector=2001+2004+2005+2006; %let k=4;

 


data t2001;
input id y;
1 10
2 20
;
Run;

data t2004;
input id y;
1 11
2 21
;
Run;

data t2005;
input id y;
1 15
2 25
;
Run;

data t2006;
input id y;
1 18
2 27
;
Run;

%macro RRR;
create table summary&YYMM.  as
select sum(Y) as  sum_Y_&YYMM.
from  t&YYMM.
;
quit;
%mend RRR;

/*Way1 to run the macros*/
%RRR(2001);
%RRR(2004);
%RRR(2005);
%RRR(2006);

/*Way2 to run the macros*/
%let vector=2001+2004+2005+2006;
%let k=4;
Now need to run automatically for each argument in vector macro parameter
What is the way to do it please?
1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

You can code:

%let vector = 2001 2004 2005 2006;
%macro run_all;
    %let n = %sysfunc(countw("&vector")); /* argument can be either quoted or not */
    %do i=1 %to &n;
          %let mm = %scan(&vector,&i);
          %RRR(&mm);
    %end;
%mend run_all;
%run_all;

 

View solution in original post

4 REPLIES 4
andreas_lds
Jade | Level 19

Loop over &vector, extract each year with scan-function, call macro rrr for each extracted value. I am 100% sure, that this has been asked and answered already, e.g. https://communities.sas.com/t5/SAS-Programming/conditionally-import-no-output/m-p/614550 - wait that was a question asked by yourself.

Ronein
Onyx | Level 15

Thank you

Shmuel
Garnet | Level 18

You can code:

%let vector = 2001 2004 2005 2006;
%macro run_all;
    %let n = %sysfunc(countw("&vector")); /* argument can be either quoted or not */
    %do i=1 %to &n;
          %let mm = %scan(&vector,&i);
          %RRR(&mm);
    %end;
%mend run_all;
%run_all;

 

Ronein
Onyx | Level 15

Great,

so as I understand in this case need to define 2 macros.

first Macro to perform the summary statistics from source data sets

Second Macro to Run the first macro on the relevant data sets

Thank you

 

%macro RRR;							
create table summary&YYMM.  as							
select sum(Y) as  sum_Y_&YYMM.							
from  t&YYMM.							
;							
quit;							
%mend RRR;							
							
%let vector=2001+2004+2005+2006;							
%let k = %sysfunc(countw(&vector));							
%macro RUN_RRR; 							
%do j=1 %to &k.;							
%let YYMM=%scan(&vector.,&j.,+);							
%RRR(&YYMM.);							
%end;							
%mend RUN_RRR;							
%RUN_RRR;							
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1720 views
  • 1 like
  • 3 in conversation