BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Tommer
Obsidian | Level 7

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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

--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

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

--
Paige Miller
ballardw
Super User

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;

 

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
  • 2 replies
  • 754 views
  • 2 likes
  • 3 in conversation