BookmarkSubscribeRSS Feed
ChHUA
Calcite | Level 5

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;

 

 

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

 

WarrenKuhfeld
Rhodochrosite | Level 12

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.

Astounding
PROC Star

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

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
  • 3 replies
  • 684 views
  • 0 likes
  • 4 in conversation