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;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.