Hello everyone,
I hope you are going well.
Can someone explain me why this is a infinite loop:
%MACRO TEST;
%LET A=3;
%LET B=6;
%IF &B >= &A %THEN %DO;
%DO %WHILE(A<B);
%PUT SOME TEXT OR INSTRUCTIONS;
%LET A=%EVAL(&A+1);
%END;
%END;
%MEND TEST;
%TEST;
And how to build a macro program to achieve the following:
- Perform some instructions if B>A and increment A by 1.
- Repeat this instruction until A=B
Thank you.
do while (&a<&b);
You forgot & signs in your
%DO %WHILE(A<B);
If you add the &, looks like it's doing what you want:
%MACRO TEST;
%local A B ;
%LET A=3;
%LET B=6;
%IF &B >= &A %THEN %DO;
%DO %WHILE(&A<&B);
%PUT &=A &=B SOME TEXT OR INSTRUCTIONS;
%LET A=%EVAL(&A+1);
%END;
%END;
%MEND TEST;
Because the letter A is always smaller than the letter B in lexical ordering.
Hi @Hugo_B While others have answered or given you the needed correction.
Can someone explain me why this is a infinite loop:
Since we know macro language processes text, it's clear in an ascii or ebcidic collating sequence A is less than B. Your do while expression evaluates at the top of the loop which always is true. That's what is the cause of your infinite loop. In your case, you have two letters and not two macro variable references. This is a great teaser to many who won't take notice.
Look at this example-->
%macro t;
%if b<a %then %put true;
%else %put false;
%mend t;
%t
@Hugo_B wrote:
Thank you, I didn't even know that it was possible two compare letters or strings.
Just don't expect results to always be as expected when numerals in a string (character) value are involved and Upper case come before Lower case. '1000' is "less than" '9' because character comparisons use character by character comparisons. Since the first 1 in '1000' is before '9' then it is "less".
Also 'a' is "greater than" 'Z' (ALL lower case come after all uppercase letters)
data _null_; if 'a' < 'Z' then put 'a is less than Z'; else if 'a' > 'Z' then put 'a is greater than Z'; run;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.