DATA Step, Macro, Functions and more

Do loop

Reply
Contributor
Posts: 24

Do loop

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.
PROC Star
Posts: 7,363

Re: Do loop

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
PROC Star
Posts: 7,363

Re: Do loop

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
Contributor
Posts: 24

Re: Do loop

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?
PROC Star
Posts: 7,363

Re: Do loop

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
Contributor
Posts: 24

Re: Do loop

Thanks for pointing that out. Now I know what it is doing.
Valued Guide
Posts: 632

Re: Do loop

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.
Super Contributor
Super Contributor
Posts: 3,174

Re: Do loop

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.
Valued Guide
Posts: 632

Re: Do loop

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
Contributor
Posts: 24

Re: Do loop

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!
Super Contributor
Super Contributor
Posts: 3,174

Re: Do loop

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.
Ask a Question
Discussion stats
  • 10 replies
  • 213 views
  • 0 likes
  • 4 in conversation