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.
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
Thus, if you use do i=20 to 1 by -1, it will work as expected.
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?
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.
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.