data work.sale2;
do Month = 1 to 12;
X + 1;
end;
run;
proc print data = work.sale2;
run;
* the value of month is 12 ;
data work.sale2;
do Month = 1 to 12;
X + 1;
output;
end;
run;
proc print data = work.sale2;
run;
* the value of month is 13
the value of month in the the two sas data set is different, Why ?
In both DATA steps, the final value of MONTH is 13. Inside the loop it goes from 1 to 12. At the END statement for the 12th time, SAS increases the value to 13 and then notices that the loop is over.
When the DATA step contains an OUTPUT statement, the only time an observation gets output is when the OUTPUT statement executes. So with no OUTPUT statement, the observation gets output at the end of all the programming logic when MONTH is 13. Once the OUTPUT statement is added, the outputting occurs inside the loop, before reaching the end of the logic, and before increasing MONTH to 13.
HI @tianerhu Please run the below, check the LOG and you tell us why 😊
data work.sale2;
do Month = 1 to 12;
X + 1;
put month=;
end;
put month=;
run;
When you do not have an Output statement the SAS data step uses an implied "output" at the bottom or end of the step.
If you only have on "end" of the data step you only get one record.
Your layout of your question is confusing:
data work.sale2;
do Month = 1 to 12;
X + 1;
end;
run;
will only generate one output record. (the second output example).
Your Second data step
data work.sale2; do Month = 1 to 12; X + 1; output; end;
will generate 12 records (the First proc print result you show).
The Output statement is executable when means when one is encountered in the code the current data is written to the output. When you add any explicit output statement then you have told SAS that you are controlling when/where the output data is to go.
You can have multiple output statements in a single data step to create additional records. You can have output statements to specify which data set to write to when.
Consider this code. See the values
Data example; do i=1 to 3; output;
x=2+i; output;
y=3+i; output;
z=4+i; end; run;
Work out the results. When you figure out why any particular value is in the output you'll have learned an important lesson.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.