BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAS_inquisitive
Lapis Lazuli | Level 10

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;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

6 REPLIES 6
Astounding
PROC Star

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.

SAS_inquisitive
Lapis Lazuli | Level 10

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

 

Reeza
Super User

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;

 

SAS_inquisitive
Lapis Lazuli | Level 10

@Reeza . Thanks. This is helpful.

Astounding
PROC Star

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.

SAS_inquisitive
Lapis Lazuli | Level 10

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

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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