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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 6 replies
  • 1170 views
  • 5 likes
  • 3 in conversation