BookmarkSubscribeRSS Feed
Naive_help
Calcite | Level 5

I have two programs - 

DATA A1;
DO I=1 TO 10;
X+1;
OUTPUT;
END;
RUN;

 

Created the data set as 

Naive_help_0-1662769287371.png

2)

DATA A2;
DO I=1 TO 10;
i+1;
X+1;
OUTPUT;
END;
RUN;

created the data set as 

Naive_help_1-1662769372284.png

I would appreciate it if you could explain how i+1 statement works on the second program.

Thanks!

 

5 REPLIES 5
Kurt_Bremser
Super User

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.

kumar75
Calcite | Level 5

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

Kurt_Bremser
Super User

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.

kumar75
Calcite | Level 5
Thank you!
Tom
Super User Tom
Super User

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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!

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
  • 5 replies
  • 882 views
  • 2 likes
  • 4 in conversation