SAS Procedures

Help using Base SAS procedures
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-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 13700 views
  • 7 likes
  • 4 in conversation