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;
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.
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.
@ 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;
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;
@Reeza . Thanks. This is helpful.
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.
@Astounding. Thanks. This is what I was assuming.
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!
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.