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

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 🙂

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

Because the else if statement is only executed if the initial if statement is not true 🙂

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Ksharp
Super User
It is overwrited by this :
if Price ge 12.99 then Price_Gp="A";

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 3 replies
  • 906 views
  • 1 like
  • 4 in conversation