BookmarkSubscribeRSS Feed
Milo08
Calcite | Level 5

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!  

4 REPLIES 4
Tom
Super User Tom
Super User

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.

Tom
Super User Tom
Super User

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 
Milo08
Calcite | Level 5

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

Milo08
Calcite | Level 5

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1653 views
  • 6 likes
  • 2 in conversation