BookmarkSubscribeRSS Feed
tianerhu
Pyrite | Level 9
data work.sale2;
 do Month = 1 to 12;
	    X + 1;
     end;
	 
run;
proc print data = work.sale2;
run;

tianerhu_1-1619655246486.png

* 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;

tianerhu_2-1619655285430.png

 

* the value of month is 13

the value of month in the the two sas data set is different, Why ?

3 REPLIES 3
Astounding
PROC Star

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.

novinosrin
Tourmaline | Level 20

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;
ballardw
Super User

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: Call for Content

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!

Submit your idea!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 469 views
  • 0 likes
  • 4 in conversation