DATA Step, Macro, Functions and more

output statement

Accepted Solution Solved
Reply
Regular Contributor
Posts: 234
Accepted Solution

output statement

Why does the second  program (want2) create missing flag variable?

 

data want1;
	set SASHELP.CLASS (WHERE= (NAME in ("Alfred","Alice")));

	if Sex ='M' then
		Flag = 1;

	if Name = "Alfred" then
		output;

	if Name = "Alice" then
		output;
run;

data want2;
	set SASHELP.CLASS (WHERE= (NAME in ("Alfred","Alice")));

	if Name = "Alfred" then
		output;

	if Name = "Alice" then
		output;

	if Sex ='M' then
		Flag = 1;
run;

 

 


Accepted Solutions
Solution
‎07-19-2016 09:55 PM
Super User
Posts: 5,099

Re: output statement

There are two factors at work.

 

First, the statements in the DATA step execute in order.

 

Second, the OUTPUT statement does not mean output at the end of the DATA step.  It means output right now, before doing any more work.  So the OUTPUT statement executes before assigning a value to FLAG.

 

FLAG is still part of the output because SAS has already looked through your statements to see what variables it needs to create.  But it has a missing value because it is output before the final IF/THEN statement executes.

View solution in original post


All Replies
Solution
‎07-19-2016 09:55 PM
Super User
Posts: 5,099

Re: output statement

There are two factors at work.

 

First, the statements in the DATA step execute in order.

 

Second, the OUTPUT statement does not mean output at the end of the DATA step.  It means output right now, before doing any more work.  So the OUTPUT statement executes before assigning a value to FLAG.

 

FLAG is still part of the output because SAS has already looked through your statements to see what variables it needs to create.  But it has a missing value because it is output before the final IF/THEN statement executes.

Regular Contributor
Posts: 234

Re: output statement

[ Edited ]

@ Astounding, Thanks for clarification.  So only that observation is outputted (output statment) even there are other observations in the input data set, right?  In my example the input data set has 2 observations, but only one observation meets the criteria in the output statement.

data want1;
	set SASHELP.CLASS (WHERE= (NAME in ("Alfred","Alice")));

	if Name = "Alfred" then
		output;

/* 	if Name = "Alice" then */
/* 		output; */
run;

 

Super User
Posts: 17,963

Re: output statement

That's not the correct way of thinking about it. 

 

SAS loops through each row. So first iteration, first IF condition is met so the record is output. On second iteration, Second IF condition is met so the record is output. 

 

Consider the following:

 

data want; 

 

set sashelp.class ;

where name in ('Alice' 'Alfred');

 

if name='Alice' then output;

 

if sex='F' then output;

 

if name='Alfred' then output;

 

run;

 

Regular Contributor
Posts: 234

Re: output statement

@Reeza . Thanks. This is helpful.

Super User
Posts: 5,099

Re: output statement

I'm not sure if this helps or not, but there is one additional implication of the OUTPUT statement.  When a DATA step contains an OUTPUT statement, the only time an observation gets output is when the OUTPUT statement executes.

Regular Contributor
Posts: 234

Re: output statement

@Astounding.  Thanks. This is what I was assuming.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 336 views
  • 5 likes
  • 3 in conversation