DATA Step, Macro, Functions and more

What is wrong with these IF and ELSE IF statements?

Reply
Occasional Contributor
Posts: 13

What is wrong with these IF and ELSE IF statements?

[ Edited ]
data ques7;
set files.bicycles;
if model eq 'Road Bikes' and unitcost gt 2500;
else if model eq 'Hybrids' and unitcost gt 660;
run;
proc print data=ques7;
run;

Untitled.pngks.png

Super User
Posts: 2,078

Re: What is wrong with these IF and ELSE IF statements?

see if this works

data ques7;
set files.bicycles;
if (model eq 'Road Bikes' and unitcost gt 2500) or ( model eq 'Hybrids' and unitcost gt 660);
run;

proc print data=ques7;
run;
 
Occasional Contributor
Posts: 13

Re: What is wrong with these IF and ELSE IF statements?

Posted in reply to novinosrin

h sd.png 

 

It shows this.

Super User
Super User
Posts: 8,290

Re: What is wrong with these IF and ELSE IF statements?

To use an ELSE statement you need to have an IF/THEN statement.

 

The first statement is just a subsetting IF statement.  It does not contain the THEN keyword.

Occasional Contributor
Posts: 13

Re: What is wrong with these IF and ELSE IF statements?

Then what would be the correct code for this? I have spent quite a lot of time over this.

Thanks.

 

Super User
Posts: 24,027

Re: What is wrong with these IF and ELSE IF statements?

@novinosrin answer is correct. If you tried it and its not giving you the expected results post your exact code and log.

Occasional Contributor
Posts: 13

Re: What is wrong with these IF and ELSE IF statements?

data ques7;
set files.bicycles;
if (model eq 'Road Bikes' and unitcost gt 2500) or ( model eq 'Hybrids' and unitcost gt 660);
run;
proc print data=ques7;
run;

I copied and pasted it exactly as told by @novinosrin. Here is how the log looks like.

 

vvkhfv.png

Super User
Super User
Posts: 8,290

Re: What is wrong with these IF and ELSE IF statements?

Sounds like you are having an issue with understanding how to express a complex condition and your confusion about SAS syntax is secondary to that.

Think about what these words mean:

image.png

 

Occasional Contributor
Posts: 13

Re: What is wrong with these IF and ELSE IF statements?

The question provides us with two conditions. Let the first one be X and the second one be Y.

We have to find  X OR Y.

 

I guess SAS goes through the dataset line by line and whichever condition (X/Y) holds true first, it gets printed in the output.

Am I right?

Super User
Posts: 24,027

Re: What is wrong with these IF and ELSE IF statements?

No, I think you have some other issue going on. Most likely a case issue, for example Road bike is not the same as Road Bike. 

Can you post a sample of what your data looks like?

 

How many records are you expecting as output?

 

You could also create flags to see which conditions are met:

 

if model='Road Bike' and unitCost>2500 then flag=1;
else if model='Hybrid' and unitCost>6500 then flag=2;
else flag=0;

Then run a PROC FREQ on the flag variable:

 

proc freq data=ques7;
table flag;
run;

 


b384b7 wrote:

The question provides us with two conditions. Let the first one be X and the second one be Y.

We have to find  X OR Y.

 

I guess SAS goes through the dataset line by line and whichever condition (X/Y) holds true first, it gets printed in the output.

Am I right?


 

Occasional Contributor
Posts: 13

Re: What is wrong with these IF and ELSE IF statements?

Thank You so much Reeza. I was wrongly putting 'Road Bikes' instead of 'Road Bike'. Also same with the Hybrid.

It was a case error. Now the code works.

data ques7;
set files.bicycles;
if (model eq 'Road Bike' and unitcost gt 2500) or ( model eq 'Hybrid' and unitcost gt 660);
run;
proc print data=ques7;
run;

qaq.png

Contributor
Posts: 40

Re: What is wrong with these IF and ELSE IF statements?

Hi,

your code looks good but you have to follow some programming rules.

 

Here you are just extracting the data based on your requirements.

 

But when you use else we have to write the then statement after the if.

 

Example:

 

if condition then output/flag;

else output/flag;

 

data ques7;

set files.bicycles;

if (model eq 'Road Bikes' and unitcost gt 2500) or (if model eq 'Hybrids' and unitcost gt 660);

run;

 

proc print data=ques7;

run; 

Frequent Contributor
Posts: 112

Re: What is wrong with these IF and ELSE IF statements?

data ques7;
set files.bicycles;
if model eq 'Road Bikes' and unitcost gt 2500;
if model eq 'Hybrids' and unitcost gt 660;
run;

/*your if and else if do not have to be in ladder*/

proc print data=ques7;
run;

Please let us know if it works

Super User
Posts: 24,027

Re: What is wrong with these IF and ELSE IF statements?

Posted in reply to Satish_Parida

@Satish_Parida that wouldn't work. The first subsetting IF would restrict the records and the second would never have a chance at being true.

Ask a Question
Discussion stats
  • 13 replies
  • 163 views
  • 0 likes
  • 6 in conversation