DATA Step, Macro, Functions and more

A Question about the Combination of DO-Loop and OUTPUT

Reply
Senior User
Posts: 1

A Question about the Combination of DO-Loop and OUTPUT

When I executed codes below, I got 11 in variable i. Why it is not 10? Could you kindly explain a little more regarding to how SAS works in this case? Thanks!

 

data a;
   do i = 1 to 10;
   end;
   output;
run;

proc print data=a;
run;

 

 

Super User
Super User
Posts: 9,402

Re: A Question about the Combination of DO-Loop and OUTPUT

The do loop value is incremented by the step value (default is 1) at the encounter of an end statement.  Therefore the end result of your code is one more than the do loop, and that is output.  The code within the do loop is only executed until incrementor is above the high limit.

 

You can read the documentation on do loops:

https://support.sas.com/documentation/cdl/en/statug/63033/HTML/default/viewer.htm#statug_odsgraph_se...

 

SAS Super FREQ
Posts: 497

Re: A Question about the Combination of DO-Loop and OUTPUT

i is set to 1, and 1 is less than or equal to 10 so the loop continues

....

i is set to 10, and 10 is less than or equal to 10 so the loop continues

i is set to 11, and 11 is not less than or equal to 10 so the code in the loop is not executed and SAS goes on to the next statement.

i is 11 at output time.

Super User
Posts: 6,628

Re: A Question about the Combination of DO-Loop and OUTPUT

While the first couple of responses outline the rules, here are a couple of DATA steps you can play with to try to get the idea:

 

data _null_;

   do k=1 to 7 by 4;

      put k=;

   end;

   put k=;

run;

 

data _null_;

   do k=1 to 9 by 4;

      put k=;

      k = k + 1;

      put k=;

   end;

   put k=;

run;

Ask a Question
Discussion stats
  • 3 replies
  • 98 views
  • 0 likes
  • 4 in conversation