DATA Step, Macro, Functions and more

MACRO within a MACRO problems.

Reply
Occasional Contributor
Posts: 14

MACRO within a MACRO problems.

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!  

Super User
Super User
Posts: 6,500

MACRO within a MACRO problems.

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.

Super User
Super User
Posts: 6,500

Re: MACRO within a MACRO problems.

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 
Occasional Contributor
Posts: 14

MACRO within a MACRO problems.

Thanks again Tom!  I removed the %PUT statement and the macro calls worked as they should have!

Occasional Contributor
Posts: 14

MACRO within a MACRO problems.

Ha...  Confused is exactly what I was!  :-)  Moving the calculate macro outside of loop was exactly what I needed to do!  Thanks Tom! 

Ask a Question
Discussion stats
  • 4 replies
  • 210 views
  • 6 likes
  • 2 in conversation