BookmarkSubscribeRSS Feed
JatinRai
Obsidian | Level 7
Hi. I've a question regarding do loop.

I experimented following simple loop:

do i=20 to 1;
put _all_;
run;

I expected this loop to run at least once because the first value of i=20 is a satisfied condition for running the loop. But it didn't execute even once. I was wondering that if the statement is do i=1 to 20 then it does run for i=20. Therefore, ideally it should run in the first case also.
10 REPLIES 10
art297
Opal | Level 21
Jatin,

Since the loop will only run while the from is <= the to integer (after first considering the increment), it never passes the first iteration. If you change it to:

do i=20 to 1 by -1;

I think it will function as expected.

HTH,
Art
art297
Opal | Level 21
For some reason my full post didn't appear.

The documentation states:
Explanation: When both start and stop are present, execution continues (based on
the value of increment) until the value of index-variable passes the value of stop.
When only start and increment are present, execution continues (based on the
value of increment) until a statement directs execution out of the loop, or until a
WHILE or UNTIL expression that is specified in the DO statement is satisfied.
If neither stop nor increment is specified, the group executes according to the
value of start. The value of stop is evaluated before the first execution of the
loop.

Thus, if you use do i=20 to 1 by -1, it will work as expected.

HTH,
Art
JatinRai
Obsidian | Level 7
Hi Art

Thanks for the response. There is no doubt that applying the increment of -1 would make it work but I was curious to know what happens if I don't provide an increment value (i.e. default=1).

The program control reads the first statement which initializes i=20, then checks whether the start condition is met or not and finds that it is met. Therefore I expected it to run at least once. But it is not doing that. In the second iteration it finds that i=21 which isn't satisfying the condition. Therefore, it'd not enter the loop. But why isn't it entering the first loop?
art297
Opal | Level 21
You overlooked the last line from the documentation:

"The value of stop is evaluated before the first execution of the loop."

20 is already greater than 1, thus the loop never executes.

Art
JatinRai
Obsidian | Level 7
Thanks for pointing that out. Now I know what it is doing.
ArtC
Rhodochrosite | Level 12
This is a change in behaviour from earlier versions. Before SAS9 the loop would have executed once (evaluated at the bottom), however by SAS9.2 (I do not remember when the change took place, but I think that it was sas9.1) a check was added to prevent the first pass of impossible loops.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Behavior change? At least not since SAS 6.09 as just tested - same occurs with this code in SAS v8 as well as SAS 9:

DATA _NULL_;
DO I=2 TO 1;
PUT 'Looping....' i=;
END;
STOP;
RUN;

The above code does not execute the PUT statement in any SAS version since 6.09.

However, using DO with UNTIL(

Scott Barry
SBBWorks, Inc.
ArtC
Rhodochrosite | Level 12
Well it must be an end of year memory glitch then. Thanks Scott, I no longer have earlier versions to play with.
Happy New Year
JatinRai
Obsidian | Level 7
Hi Scott

That was a great experiment. Thanks for running that in earlier versions.

I just found on support.sas that if the 'by' option has a non negative parameter then for the loop to run even once the initial value has to be mandatory larger than the final value.

Sas is different than other languages. In Java as far as I remember it'd have run once but SaS is different.

Anyways, thanks to you guys a lot for your inputs. HAPPY NEW YEAR!
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
The default-value for "BY" on a DO statement is essentially +1, when not coded. Having a non-negative (positive) value with an initial value greater than the ending value would not execute the DO loop, as has been demonstrated with this discussion thread in mind.

Scott Barry
SBBWorks, Inc.

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
  • 10 replies
  • 1144 views
  • 0 likes
  • 4 in conversation