Hello!
I have a simple macro do loop where i'm trying to repeat the same proc summary and print to a dataset for each iteration of my specified variable. The below code is what I have written, it does not produce an error however it stays stuck on the first macro var and repeats over and over again.
%MACRO SUMMARY;
%LET i=1;
%LET REGIONVAR=NDR KKR MSR HAM;
%LET REGION= %SCAN(®IONVAR,&i); %PUT REGION;
%DO %WHILE (®ION NE );
PROC SUMMARY DATA=®ION;
VAR HIB_COMPLETE HIB2_COMPLETE RTV_COMPLETE DTP_COMPLETE DTP3_COMPLETE OPV_COMPLETE MMR_COMPLETE HBV_COMPLETE
VAX_COMPLETE PCV_COMPLETE PCV2_COMPLETE FLU_COMPLETE FLU3_COMPLETE FLU_6MONTHS HAV_COMPLETE HAV2_COMPLETE COMPLETE431FS314 HBV_BD_COMPLETE;
OUTPUT OUT=CI MEAN= LCLM= UCLM=/AUTONAME;
RUN;
DATA ®ION._CI;
IF _N_=1 THEN SET CI(DROP=_TYPE_ _FREQ_);
SET ®ION;
RUN;
%END;
%MEND SUMMARY;
%SUMMARY (NDR KKR MSR HAM);
the log is just this over and over, which is correct, but I also need this result for KKR, MSR, HAM etc.
NOTE: There were 192754 observations read from the data set WORK.NDR.
NOTE: The data set WORK.CI has 1 observations and 56 variables.
NOTE: PROCEDURE SUMMARY used (Total process time):
real time 0.12 seconds
cpu time 0.51 seconds
NOTE: There were 1 observations read from the data set WORK.CI.
NOTE: There were 192754 observations read from the data set WORK.NDR.
NOTE: The data set WORK.NDR_CI has 192754 observations and 114 variables.
NOTE: DATA statement used (Total process time):
real time 0.17 seconds
cpu time 0.18 seconds
Any insight would be helpful, thank you!
You have the intention to loop over the 4 regions, but
Here is what I think you are trying to do:
%MACRO SUMMARY (regions);
%do i=1 %to %sysfunc(countw(®ions));
%LET REGION= %SCAN(®IONS,&i);
%put ®ion;
PROC SUMMARY DATA=®ION;
VAR HIB_COMPLETE HIB2_COMPLETE RTV_COMPLETE DTP_COMPLETE DTP3_COMPLETE OPV_COMPLETE MMR_COMPLETE HBV_COMPLETE
VAX_COMPLETE PCV_COMPLETE PCV2_COMPLETE FLU_COMPLETE FLU3_COMPLETE FLU_6MONTHS HAV_COMPLETE HAV2_COMPLETE COMPLETE431FS314 HBV_BD_COMPLETE;
OUTPUT OUT=CI MEAN= LCLM= UCLM=/AUTONAME;
RUN;
DATA ®ION._CI;
IF _N_=1 THEN SET CI(DROP=_TYPE_ _FREQ_);
SET ®ION;
RUN;
%END;
%MEND SUMMARY;
%SUMMARY (NDR KKR MSR HAM);
BTW you have
%put REGION;
which only writes the word "REGION". In the above, note I have
%put ®ion
which will write the VALUE of the macrovar named region.
And for experimentation, replace it with
%put &=region;
You have the intention to loop over the 4 regions, but
Here is what I think you are trying to do:
%MACRO SUMMARY (regions);
%do i=1 %to %sysfunc(countw(®ions));
%LET REGION= %SCAN(®IONS,&i);
%put ®ion;
PROC SUMMARY DATA=®ION;
VAR HIB_COMPLETE HIB2_COMPLETE RTV_COMPLETE DTP_COMPLETE DTP3_COMPLETE OPV_COMPLETE MMR_COMPLETE HBV_COMPLETE
VAX_COMPLETE PCV_COMPLETE PCV2_COMPLETE FLU_COMPLETE FLU3_COMPLETE FLU_6MONTHS HAV_COMPLETE HAV2_COMPLETE COMPLETE431FS314 HBV_BD_COMPLETE;
OUTPUT OUT=CI MEAN= LCLM= UCLM=/AUTONAME;
RUN;
DATA ®ION._CI;
IF _N_=1 THEN SET CI(DROP=_TYPE_ _FREQ_);
SET ®ION;
RUN;
%END;
%MEND SUMMARY;
%SUMMARY (NDR KKR MSR HAM);
BTW you have
%put REGION;
which only writes the word "REGION". In the above, note I have
%put ®ion
which will write the VALUE of the macrovar named region.
And for experimentation, replace it with
%put &=region;
You have programmed an infinite loop. Your %DO WHILE() condition is going to always be true because there is nothing inside the loop to change the value of ®ION.
Since you have the list of values up front just use a normal %DO loop instead. You can use the COUNTW() function (wrapped in the %SYSFUNC() macro function) to set the upper bound for the loop.
%DO I=1 %to %sysfunc(countw(®IONVAR,%str( )));
%LET REGION= %SCAN(®IONVAR,&i,%str( ));
...
%END;
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.