I am a relative novice at SAS coding. I am attempting to write a MACRO that will continue to call another MACRO until all calculations over a certain threshold have been removed. After each record gets removed over the threshold I need to re-calculate. The basic structure looks something similar to this.
%MACRO Loop (var1, var2);
%let stop = 1;
%Do %until (&stop = 0);
%MACRO Calculate (var3, var4, var5, var6, var7, var8);
<Make calculations>
%MEND Calculate;
PROC SQL;
SELECT cat('%MH_DIF (', var3, ', ', trim(var4), ', ', trim(var5), ', ', var6, ', ',
trim(var7), ', ', trim(var8), ' ', ');') into :List separated by ' '
FROM AllIData
WHERE X = &var1 and Y = "&var2";
quit;
%put &list;
proc sort data = &var1_&var2;
by descending Calculation;
run;
PROC SQL;
SELECT calculation, item into :maxvalue, :max
FROM &var1_&var2. (OBS=1)
quit;
%if &maxvalue > 1.5 %then
%do;
data rejects1;
set &var1_&var2;
where var5 = &max;
run;
Proc append base = rejects new = rejects1 force;
run;
data AllIData;
set AllIData;
where var5 ne &max;
run;
%end;
%else %let stop = 0;
%end;
%Mend Loop;
%Loop (var1, var2);
The big problems I am noticing are that when I mend my macros there are no lines underneath the MEND statement. Furthermore, when I look at the code underneath the MEND Calculate statement I am seeing color in the code, which should not be there for a MACRO. Finally, after I run the code, I notice in the code editor: * PROC SQL RUNNING after the name of the program. When I separate the two MACROs and run them independently, they run fine. And help would be greatly appreciated!!! Thanks in advance!
Not sure what your actual issue is, but your program structure looks confused. You should move the definition of the macro CALCULATE outside of the definition of the macro LOOP. You should not need to redefine the macro CALCULATE for every iteration inside of LOOP. You just need to call it with different parameter values or input data.
The line that looks like it might cause trouble is
%put &list;
You appear to have built the macro variable LIST as one or more calls to the macro MH_DIFF. If that macro expects to generate SAS statements the first one will be "eaten" by the %PUT statement.
If you just want to see the value of the macro variable LIST then use
%put %superq(list);
If you want to actually run the macro calls generated into the macro variable then use
&list
Thanks again Tom! I removed the %PUT statement and the macro calls worked as they should have!
Ha... Confused is exactly what I was! 🙂 Moving the calculate macro outside of loop was exactly what I needed to do! Thanks Tom!
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 16. 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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: