Hi everyone,
I have been trying to execute this macro.
The value of regions: DL+UTT EAST GJ HR MH+ROM MP+CG PB+HP RJ SOUTH.
I have defined the region if there is a gap in the value, then it will be considered as different region. So as for my input, there are 9 regions now. Which can change over time so I have kept the Regions and NUM_REGIONS as dynamic.
/* OPTIONS MPRINT MLOGIC SYMBOLGEN; */
%MACRO EXECUTE1;
%LET COUNT1=0;
PROC SQL;
SELECT COUNT(*) INTO :COUNT1
FROM CC_BASE_01NOV24;
QUIT;
%IF &COUNT1 > 0 %THEN %DO;
PROC SQL;
SELECT DISTINCT UPCASE(REGION) INTO :REGIONS SEPARATED BY ' '
FROM CC_BASE_01NOV24
WHERE REGION NE '#N/A';
SELECT COUNT(DISTINCT REGION) INTO :NUM_REGIONS
FROM CC_BASE_01NOV24
WHERE REGION NE '#N/A';
QUIT;
%PUT Unique Regions: ®IONS;
%PUT Number of Regions: &NUM_REGIONS;
%MACRO STATEMIS1(REGION);
DATA CC_BASE_&BASE_DATE._&I.;
SET CC_BASE_01NOV24(WHERE=(UPCASE(STRIP(REGION))="®ION."));
RUN;
%MEND STATEMIS1;
%DO I = 1 %TO &NUM_REGIONS;
%LET REGION = %SCAN(®IONS, &I, %STR( ));
%STATEMIS1(®ION);
%END;
%END;
%ELSE %DO;
%PUT No records found in base dataset. Exiting macro.;
%END;
%MEND EXECUTE1;
As I am trying to execute the code, it runs without any error or any output data and log doesn't show anything either.
I am attaching the log further:-
85 %MACRO EXECUTE1; 86 87 %PUT EXECUTE1 Macro Started!; 88 %LET COUNT1=0; 89 90 PROC SQL; 91 SELECT COUNT(*) INTO :COUNT1 92 FROM CC_BASE_01NOV24; 93 QUIT; 94 95 %IF &COUNT1 > 0 %THEN %DO; 96 97 98 PROC SQL; 99 SELECT DISTINCT UPCASE(REGION) INTO :REGIONS SEPARATED BY ' ' 100 FROM CC_BASE_01NOV24 101 /* WHERE REGION NE '#N/A'; */ 102 SELECT COUNT(DISTINCT REGION) INTO :NUM_REGIONS 103 FROM CC_BASE_01NOV24 104 /* WHERE REGION NE '#N/A'; */ 105 QUIT; 106 107 %PUT Unique Regions: ®IONS; 108 %PUT Number of Regions: &NUM_REGIONS; 109 110 111 112 113 %MACRO STATEMIS1(REGION); 114 %PUT ®ION.; 115 DATA CC_BASE_&BASE_DATE._&I.; 116 SET CC_BASE_01NOV24(WHERE=(UPCASE(STRIP(REGION))="®ION.")); 117 RUN; 118 119 %MEND STATEMIS1; 120 121 122 %DO I = 1 %TO &NUM_REGIONS; 123 %LET REGION = %SCAN(®IONS, &I, %STR( )); 124 %STATEMIS1(®ION); 125 %END; 126 %END; 127 %ELSE %DO; 128 %PUT No records found in base dataset. Exiting macro.; 129 %END; 130 %MEND EXECUTE1;
I want this code to execute for all the unique regions in the set. But it isn't letting me.
Can I please get help as I have checked in individual steps that,
COUNT1 has 15Lakh values in it, so it is working right.
CC_BASE_01NOV24 has 9 unique regions so the loop should work 9 times for all the regions.
What is it that I am doing wrong and how can I correct it?
Thanks You.
You have not instructed SAS to execute the macro. You have only defined the macro.
You do this by this command
%EXECUTE1
As a side issue, defining a macro within another macro definition is not a good practice. Better to define the macros separately, consecutively.
%macro execute1;
/* code for macro execute1 goes here */
%mend execute;
%macro statemis1(region);
/* code for macro statemis1 goes here */
%mend statemis1;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
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.
Ready to level-up your skills? Choose your own adventure.