BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Hugo_B
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
6 REPLIES 6
PaigeMiller
Diamond | Level 26
do while (&a<&b);
--
Paige Miller
Quentin
Super User

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;

 

Tom
Super User Tom
Super User

Because the letter A is always smaller than the letter B in lexical ordering.

novinosrin
Tourmaline | Level 20

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
Obsidian | Level 7
Thank you, I didn't even know that it was possible two compare letters or strings.
ballardw
Super User

@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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1941 views
  • 7 likes
  • 6 in conversation