Hi,
The result of code1 and code2 are same. However, when I use the output statement before "end" the result is different in code3. I am trying to understand the logic of how this works when I have the "end" before and after the output statement.
/*Code1*/
data class;
set sashelp.class;
output;
if sex="M" then Sex1 = 1;
output;
run;
/*Code1*/
data class;
set sashelp.class;
output;
if sex="M" then do;
Sex1 = 1;
Sex2="Male";
end;
output;
run;
/*Code3*/
data class;
set sashelp.class;
output;
if sex="M" then do;
Sex1 = 1;
Sex2="Male";
output;
end;
run;
Code 3 does the second output step only when sex="M"
Code 2 always does the second output step, whether sex="M" or not, because the output statement is not inside the IF-THEN DO-END
Code 3 does the second output step only when sex="M"
Code 2 always does the second output step, whether sex="M" or not, because the output statement is not inside the IF-THEN DO-END
Indenting code consistently and with reasonable rules can often help resolve such timing issue questions. And pasting code or log entries into a code box opened on the forum with the </> will preserve that indenting while the main message windows will remove characters.
/*Code1*/ data class; set sashelp.class; output; if sex="M" then Sex1 = 1; output; run; /*Code2*/ data class; set sashelp.class; output; if sex="M" then do; Sex1 = 1; Sex2="Male"; end; output; run; /*Code3*/ data class; set sashelp.class; output; if sex="M" then do; Sex1 = 1; Sex2="Male"; output; <= Easy to see @PaigeMiller's comment about conditional output. end; run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.