BookmarkSubscribeRSS Feed
dooheekim01
Obsidian | Level 7

The question :

    • create results.output36 from cleandata36
    • Ensure that all values for variable Kilograms are between 40 and 200, inclusively.
    • If the value is missing or out of range, replace the value with the MEDIAN Kilograms value for the respective group (A,B) calculated in step 2.

The answer is :

data results.output36;
set cleandata36;
if Kilograms < 40 or Kilograms > 200 then do;
if group='A' then kilograms=79;
else kilograms=89;
end;
run;

 

My question is that:

if Kilograms <40 or Kilograms > 200 then do;

else group='A' then kilograms=79;

didn't work.

My question is why I should not use 'else' in red. It is hard to understand. Thank you so much for your help. 

4 REPLIES 4
Shmuel
Garnet | Level 18

1) DO; needs to be closed by END;

2) ELSE <do something>;

 

Your code:

if Kilograms <40 or Kilograms > 200 then DO;
   /* option 1*/ <do something is optional>; END;
else /* 40 <= Kilograms <= 200 >
  group='A' ;  /* then needs to be preceded by IF*/
   kilograms=79; /* Is this part of the IF? of the ELSE? or always ? */

If you have more than one statement to do conditionally enclose then between DO: and END;

 

dooheekim01
Obsidian | Level 7
Thank you so much, Shmuel!
anming
Pyrite | Level 9
data result.output36;
set result.cleandata36;
if 40 <= kilograms le 200 then kilograms=kilograms;
else if group ='A' then kilograms=79;
else if group ="B" then kilograms=89;

run;

alternatively, you can code like above. 'then' for one action, 'do' for multiple actions and finish with 'end'.

dooheekim01
Obsidian | Level 7
Thank you so much, Anming. It was really helpful!