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

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(&REGIONVAR,&i); %PUT REGION;

%DO %WHILE (&REGION NE );

 

PROC SUMMARY DATA=&REGION;

    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 &REGION._CI;

    IF _N_=1 THEN SET CI(DROP=_TYPE_ _FREQ_);

    SET &REGION;

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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

You have the intention to loop over the 4 regions, but

  1. you never increment i from 1 to 4 - it always states at 1, (2) your call to the macro 
  2. Your call to the macro
        %SUMMARY (NDR KKR MSR HAM);
    lists a single argument with 4 components, but yours macro declaration statement
       %macro summary;
    makes no provision for that argument.

Here is what I think you are trying to do:

 

%MACRO SUMMARY (regions);

%do i=1 %to %sysfunc(countw(&regions)); 
  %LET REGION= %SCAN(&REGIONS,&i);
  %put &region;
  PROC SUMMARY DATA=&REGION;
    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 &REGION._CI;
    IF _N_=1 THEN SET CI(DROP=_TYPE_ _FREQ_);
    SET &REGION;
  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  &region
which will write the VALUE of the macrovar named region.

 

And for experimentation, replace it with 
   %put &=region;

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

2 REPLIES 2
mkeintz
PROC Star

You have the intention to loop over the 4 regions, but

  1. you never increment i from 1 to 4 - it always states at 1, (2) your call to the macro 
  2. Your call to the macro
        %SUMMARY (NDR KKR MSR HAM);
    lists a single argument with 4 components, but yours macro declaration statement
       %macro summary;
    makes no provision for that argument.

Here is what I think you are trying to do:

 

%MACRO SUMMARY (regions);

%do i=1 %to %sysfunc(countw(&regions)); 
  %LET REGION= %SCAN(&REGIONS,&i);
  %put &region;
  PROC SUMMARY DATA=&REGION;
    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 &REGION._CI;
    IF _N_=1 THEN SET CI(DROP=_TYPE_ _FREQ_);
    SET &REGION;
  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  &region
which will write the VALUE of the macrovar named region.

 

And for experimentation, replace it with 
   %put &=region;

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Tom
Super User Tom
Super User

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 &REGION.

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(&REGIONVAR,%str( )));
  %LET REGION= %SCAN(&REGIONVAR,&i,%str( )); 
  ...
%END;

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 2840 views
  • 3 likes
  • 3 in conversation