DATA Step, Macro, Functions and more

%do %until loop resolves even though condition is false

Reply
Contributor
Posts: 20

%do %until loop resolves even though condition is false

The following loop ends after one iteration even though Max_V1>5. However the code works perfectly if I use a fraction (say 0.5) instead of a number above 1 (5 in my current code)

%do %until (&Max_V1.) le 5);

a few more lines of code goes here........then ......


PROC SQL NOPRINT;
SELECT VarianceInflation_Max INTO: Max_V1
FROM Sum_stat;
QUIT;

%if &Max_V1. le 5 %then %goto leave;

proc sql;
Select Variable INTO: Drop_Var
from Spdework.varlist where VarianceInflation = (select max(Varia) as MV
from varlist);
quit;




proc sql;
Select Variable INTO: L_F_Vars SEPARATED by ' '
From Varlist
Where Variable <> "&Drop_Var.";
quit;



%END;
%leave:
%mend auto_vif;
Regular Contributor
Posts: 165

Re: %do %until loop resolves even though condition is false

do until is evaluated at the end of the loop and do while is evaluated at the top of the loop.
Contributor
Posts: 20

Re: %do %until loop resolves even though condition is false

Thanks for yor reply. I am nit sure why SAS log displays the following message...
SYMBOLGEN: Macro variable MAX_V1 resolves to 49.809566165
MLOGIC(AUTO_VIF): %IF condition %sysevalf(&Max_V1) le 5 is TRUE
MLOGIC(AUTO_VIF): %GOTO LEAVE (label resolves to LEAVE).
MLOGIC(AUTO_VIF): Ending execution.
Super Contributor
Super Contributor
Posts: 3,174

Re: %do %until loop resolves even though condition is false

Did you interrogate the value of SAS variable VarianceInflation_Max in file WORK.Sum_stat? Note your SAS PROC SQL uses this variable to assign the macro variable.

Scott Barry
SBBWorks, Inc.
Contributor
Posts: 20

Re: %do %until loop resolves even though condition is false

Yes and it has the same value of 49. Thanks.
Super Contributor
Super Contributor
Posts: 3,174

Re: %do %until loop resolves even though condition is false

That's the value you see (SAS output format), not necessarily the internal value. Suggest you might use the BEST. format to display the SAS variable - also how is the SAS variable calculated/computed?

Scott Barry
SBBWorks, Inc.
Valued Guide
Posts: 2,175

Re: %do %until loop resolves even though condition is false

> displays the following message...
> SYMBOLGEN: Macro variable MAX_V1 resolves to 49.809566165
> MLOGIC(AUTO_VIF): %IF condition %sysevalf(&Max_V1) le 5 is TRUE

that TRUE above implies a string comparison of %sysevalf(&Max_V1) and the 5.
Perhaps the evaluation of %sysevalf(&Max_V1) needs to be done before the compare in the %do statement
Contributor
Posts: 20

Re: %do %until loop resolves even though condition is false

Yes I tried that but it does not work. THe loop works fine if I use a %do %unitl (&Max_V1 lt .9) i.e. I use any number under 1 on the R.H.S. of the inequality sign.
Super Contributor
Super Contributor
Posts: 3,174

Re: %do %until loop resolves even though condition is false

The first %DO statement shown in the original post has incorrect syntax and would generate an error. The %DO %UNTIL(...) logic inside the parentheses must be enclosed in parentheses itself and you must use %SYSEVALF() correctly.

Scott Barry
SBBWorks, Inc.

Suggested Google advanced search argument, this topic / post:

sysevalf documentation site:sas.com
Valued Guide
Posts: 632

Re: %do %until loop resolves even though condition is false

Numeric expressions are evaluated with a %EVAL (which only works with integers). If either side of your expression resolves to a non-integer, and anything with a decimal point including 1. is a non-integer, the comparison becomes alphabetic rather than numeric. Using %SYSEVALF as Scott suggests forces a numeric comparison on non-integers.
Contributor
Posts: 20

Re: %do %until loop resolves even though condition is false

if that is the case, then how does the code work whenI I use a fraction (say .1) on the RHS of the inequality. Thanks for your help.
Valued Guide
Posts: 632

Re: %do %until loop resolves even though condition is false

Consider the following snippet from my LOG:
[pre]
87 %macro test;
88 %let v=49.5;
89 %if &v lt 5 %then %put &v is less than 5;
90 %if &v gt .5 %then %put &v is GT than .5;
91 %if %sysevalf(&v gt 5) %then %put &v is GT than 5;
92 %mend test;
93 %test
49.5 is less than 5
49.5 is GT than .5
49.5 is GT than 5
[/pre]
Although the second %IF appears to evaluate correctly (numerically) it is actually doing an alphabetic comparison (decimal points sort before numbers alphabetically on my OS - windows).
Only the third expression is actually giving correct numeric results.

The key is to remember that for expressions to be evaluated numerically in the macro language they must be integer or alternatively we take charge with the %SYSEVALF function.
Art
Contributor
Posts: 20

Re: %do %until loop resolves even though condition is false

Thanks for your reply. I was able to fix the issue with the sysevalf condition in the appropriate place.
Ask a Question
Discussion stats
  • 12 replies
  • 276 views
  • 0 likes
  • 5 in conversation