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;
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.
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.
Thank you so much .
Could you explain why the variable month become 13, the same way to the variable year ?
@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.
Thank you for your help.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.