hello users !
Given the following SAS data set WORK.TOYS:
Product Group Price
--------- ----------- -----
Cards Indoors 9.99
Marbles Indoors 8.99
Drum Instruments 12.99
Hula-Hoop Outdoors 12.99
Ball Outdoors 8.49
The following SAS program is submitted:
data WORK.GROUPS; set WORK.TOYS; if Group="Outdoors" then Price_Gp="C"; if Price ge 12.99 then Price_Gp="A"; else if Price ge 8.99 then Price_Gp="B"; run;
What will be the value of Price_Gp for Hula-Hoop and Ball?
Answer is Hula-Hoop: 'A', Ball: 'C'
Why answer is not Hula-Hoop: 'C', Ball: 'C ?? can pls someone help. Thanks in advance 🙂
The line
if Price ge 12.99 then Price_Gp="A";
executes and sets the Price_Gp value to "A" for Hula-Hoop. This overwrites the value of Price_Gp="C" that was obtained in the previous line.
Since you don't want that, you need to make sure the above line does not execute for Hula-Hoop. This code would look like
data WORK.GROUPS;
set WORK.TOYS;
if Group="Outdoors" then Price_Gp="C";
ELSE if Price ge 12.99 then Price_Gp="A";
else if Price ge 8.99 then Price_Gp="B";
run;
Now, for Outdoors toys, the lines that check the Price will not be used; and for toys that are not Outdoors, the price checks are the only lines that will be used.
Because the else if statement is only executed if the initial if statement is not true 🙂
The line
if Price ge 12.99 then Price_Gp="A";
executes and sets the Price_Gp value to "A" for Hula-Hoop. This overwrites the value of Price_Gp="C" that was obtained in the previous line.
Since you don't want that, you need to make sure the above line does not execute for Hula-Hoop. This code would look like
data WORK.GROUPS;
set WORK.TOYS;
if Group="Outdoors" then Price_Gp="C";
ELSE if Price ge 12.99 then Price_Gp="A";
else if Price ge 8.99 then Price_Gp="B";
run;
Now, for Outdoors toys, the lines that check the Price will not be used; and for toys that are not Outdoors, the price checks are the only lines that will be used.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.