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!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 588 views
  • 2 likes
  • 3 in conversation