BookmarkSubscribeRSS Feed
gxu
Calcite | Level 5 gxu
Calcite | Level 5

Hi All,

I have a very basic question about the do loop and output statement.

I learned from "the little sas book" that if I put an "output" statement explicitly in the do loop, then I will get several observations.

For example:

*******************

data generate;

do x=1 to 6;

    y=x **2;

output;

end;

run;

*****************

then I will get a data set with 6 observations and 2 variables (there is no problem for this part)

**********************

obs  x   y

1     1   1

2    2   4

...

6    6  36

*********************

If I remove the ouput statement, according to the book, it will output only one observation.  I expected to get

obs  x  y

1     6  36

however, the result is:

obs x y

1   7 36

I am confused about the result of x,  why it's 7 instead of 6?  will the variable in do loop always increase even if the loop ends?

I will appreciate your comments. thanks!

5 REPLIES 5
art297
Opal | Level 21

In the statement you used, x=1 to 6, 6 only represented the stop condition.  I.e., the loop stopped when x became greater than 6 and, since you didn't specify an increment (i.e., a by value), it incremented each loop by 1.  If you don't use the from,to form of the do statement, you can get it so that it won't increment beyond the specified values.  E.g.,

data test;

do count=1,2,3,4,5,6;

end;

output;

run;

gxu
Calcite | Level 5 gxu
Calcite | Level 5

art297, I see  the reason now. thank you!

the example you gave really is really helpful.

DF
Fluorite | Level 6 DF
Fluorite | Level 6

It might help to see it using "putlog" as below:

data generate;

do x=1 to 6;

    y=x **2;

    putlog "inner " _all_;

end;

putlog "outer " _all_;

run;

I've marked the two entries of putlog to show whether they are inside or outside the loop.  This gives the below output:

inner x=1 y=1 _ERROR_=0 _N_=1

inner x=2 y=4 _ERROR_=0 _N_=1

inner x=3 y=9 _ERROR_=0 _N_=1

inner x=4 y=16 _ERROR_=0 _N_=1

inner x=5 y=25 _ERROR_=0 _N_=1

inner x=6 y=36 _ERROR_=0 _N_=1

outer x=7 y=36 _ERROR_=0 _N_=1

You only had output in place of the "inner" putlog, hence you get 6 rows of output.

As Art explained, variable X is increased one more time but the code inside your loop does not execute as the loop as now exceeded its end condition.  In your original code then, neither your output line, or calculation of Y run, hence Y remains at its previous value of 36 and the row with x=7 is not output.

Howles
Quartz | Level 8

You can have it both ways. Use the TO/BY spec along with an UNTIL condtion.

data test;

do count=1 TO 6 UNTIL (count EQ 6);

end;

output;

run;

art297 wrote:

In the statement you used, x=1 to 6, 6 only represented the stop condition.  I.e., the loop stopped when x became greater than 6 and, since you didn't specify an increment (i.e., a by value), it incremented each loop by 1.  If you don't use the from,to form of the do statement, you can get it so that it won't increment beyond the specified values.  E.g.,

data test;

do count=1,2,3,4,5,6;

end;

output;

run;

gxu
Calcite | Level 5 gxu
Calcite | Level 5

DF, Howles, thank you very much for providing the answers.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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