## the do loop and output statement

Occasional Contributor
Posts: 18

# the do loop and output statement

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?

PROC Star
Posts: 8,164

## Re: the do loop and output statement

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;

Occasional Contributor
Posts: 18

## the do loop and output statement

art297, I see  the reason now. thank you!

the example you gave really is really helpful.

Frequent Contributor
Posts: 94

## the do loop and output statement

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.

Regular Contributor
Posts: 184

## the do loop and output statement

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;`
Occasional Contributor
Posts: 18

## the do loop and output statement

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

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