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;
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;
It shows this.
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.
Then what would be the correct code for this? I have spent quite a lot of time over this.
Thanks.
@novinosrin answer is correct. If you tried it and its not giving you the expected results post your exact code and log.
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.
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:
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?
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?
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;
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;
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
@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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.