Lu Yang
Lu Yang

Reputation: 11

SAS %do %until loop

As a part of my, loops need to be done until a condition is met. I am using the code as following:

%IF &maxvif ge &thresh %THEN

%DO %UNTIL (&maxvif lt &thresh)

....

%END;

However, it could not run properly. Even when &maxvif=4.05, it still says (&maxvif lt &thresh) is false. Thus, this loop never stop.

Below is part of the log, both macro variables are resolved properly, but the final evaluation went wrong.

SYMBOLGEN: Macro variable MAXVIF resolves to 4.050694277

SYMBOLGEN: Macro variable THRESH resolves to 10

MLOGIC(BACKWARDVIF): %DO %UNTIL(&maxvif lt &thresh) condition is FALSE; loop will iterate again.

Upvotes: 1

Views: 1707

Answers (1)

Tim Sands
Tim Sands

Reputation: 1068

Your condition is being assessed as text. In a text comparison "10" < "4.05" since "1" < "4", just like "AZ" < "BB" due to "A" < "B".

To flesh out @Reeza's comment, here is a sample macro that seems to work as expected. It's a whole lot of %sysevalf:

%Macro testme(thresh,maxvif);
    %Do %until (%sysevalf(&maxvif > &thresh ));
        %Put "In Do Loop: maxvif= &maxvif -- thresh = &thresh";
        %Let maxvif = %sysevalf(&maxvif + 1);
    %End;
    %Put "--- DONE!   maxvif= &maxvif  -- thresh = &thresh";
%Mend;

%testme(7,4.05123) prints to the log:

In Do Loop: maxvif= 4.05123 -- thresh = 7
In Do Loop: maxvif= 5.05123 -- thresh = 7
In Do Loop: maxvif= 6.05123 -- thresh = 7
--- DONE!   maxvif= 7.05123 -- thresh = 7;

Upvotes: 2

Related Questions