I have two programs -
DATA A1;
DO I=1 TO 10;
X+1;
OUTPUT;
END;
RUN;
Created the data set as
2)
DATA A2;
DO I=1 TO 10;
i+1;
X+1;
OUTPUT;
END;
RUN;
created the data set as
I would appreciate it if you could explain how i+1 statement works on the second program.
Thanks!
An iterative DO loop looks like this after the compiler does its work:
/* do i = 1 to 10 */
i = 1;
start:
if i > 10 then goto end;
/* "body" of the loop */
i = i + 1;
goto start;
end:
Now, if you code an additional increment of the loop variable into the loop, both increments will be executed.
@Kurt_Bremser Thank you for the feedback. The explanation on i+1 is not clear to me. I would appreciate it if you could explain how it works on PDV.
At the startof the DO loop, i is set to 1.
Then you increment it with the i+1 sum statement, so it becomes 2, which is the value written to the output.
Then the DO loop does its own increment, setting i to 3, after which (in the next DO iteration) you again increment, so 4 is written to the output.
Repeat until i is set to 11 by the internal increment of the DO loop, which then terminates.
The sum statement:
i+1;
Performs this assignment statement.
i=sum(i,1);
In addition it flags the variable I to be retained (not reset to missing on the next iteration of the data step). And if there is no other RETAIN statement that is setting a a different initial value the variable is initialized to zero.
Since this data step only iterates once whether or not the variable is retained makes no different. Since the DO loop will overwrite any initial value of I you can replace the sum statement with a simple assignment statement instead.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Ready to level-up your skills? Choose your own adventure.