Pyrite | Level 9

## why the number of year and month is added 1 when remove the 'output'?

``````libname results 'C:\practice everyday\output';
data results.output13_2;
total = 100;
do year = 1 to 3;
do month = 1 to 12;
total + total*0.05;
output;
end;
total + 50;
end;
run;
proc print data = results.output13_2;
run;``````

libname results 'C:\practice everyday\output';
data results.output13_2;
total = 100;
do year = 1 to 3;
do month = 1 to 12;
total + total*0.05;
*output;
end;
total + 50;
end;
run;
proc print data = results.output13_2;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
PROC Star

## Re: why the number of year and month is added 1 when remove the 'output'?

In your first program you have an explicit OUTPUT statement inside the DO loop.  Therefore the normal implicit OUTPUT (just before the RUN; statement) is eliminated.  But in your second program, there is no explicit OUTPUT, so the implicit OUTPUT is executed.

So the question really is "why does the iterator YEAR in DO YEAR=1 to 3; ... END; end up as a 4"?   The iterator is always incremented (from 1 to 2, 2 to 3, 3 to 4) at the end of each iteration..  Once YEAR=4 there is no subsequent iteration of the DO loop executed, because it is now outside of the upper limit of 3.  Here's code to show that:

``````data _null_;
do year=1 to 3 ;
put 'inside ' year=;
end;
put 'after  ' year=;
run;``````

Now, if you really want to iterate from year=1 to 3 without year being incremented to 4 you can add an UNTIL expression:

``````data _null_;
do year=1 to 3 until (year=3);
put 'inside ' year=;
end;
put 'after  ' year=;
run;    ``````

This works because the UNTIL expression (telling the loop when to stop) is evaluated at the end of each iteration, but before the iterator is incremented.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
5 REPLIES 5
PROC Star

## Re: why the number of year and month is added 1 when remove the 'output'?

In your first program you have an explicit OUTPUT statement inside the DO loop.  Therefore the normal implicit OUTPUT (just before the RUN; statement) is eliminated.  But in your second program, there is no explicit OUTPUT, so the implicit OUTPUT is executed.

So the question really is "why does the iterator YEAR in DO YEAR=1 to 3; ... END; end up as a 4"?   The iterator is always incremented (from 1 to 2, 2 to 3, 3 to 4) at the end of each iteration..  Once YEAR=4 there is no subsequent iteration of the DO loop executed, because it is now outside of the upper limit of 3.  Here's code to show that:

``````data _null_;
do year=1 to 3 ;
put 'inside ' year=;
end;
put 'after  ' year=;
run;``````

Now, if you really want to iterate from year=1 to 3 without year being incremented to 4 you can add an UNTIL expression:

``````data _null_;
do year=1 to 3 until (year=3);
put 'inside ' year=;
end;
put 'after  ' year=;
run;    ``````

This works because the UNTIL expression (telling the loop when to stop) is evaluated at the end of each iteration, but before the iterator is incremented.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Pyrite | Level 9

## Re: why the number of year and month is added 1 when remove the 'output'?

Thank you so much .

Pyrite | Level 9

## Re: why the number of year and month is added 1 when remove the 'output'?

Could you explain why the variable month become 13, the same way to the variable year ?

Diamond | Level 26

## Re: why the number of year and month is added 1 when remove the 'output'?

@tianerhu wrote:

Could you explain why the variable month become 13, the same way to the variable year ?

It's the same reason as year; the loop increments month to 13, then it stops as the loop will not run when month is 13.

--
Paige Miller
Pyrite | Level 9