BookmarkSubscribeRSS Feed
b384b7
Calcite | Level 5
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

13 REPLIES 13
novinosrin
Tourmaline | Level 20

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;
 
b384b7
Calcite | Level 5

h sd.png 

 

It shows this.

Tom
Super User Tom
Super User

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.

b384b7
Calcite | Level 5

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

Thanks.

 

Reeza
Super User

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

b384b7
Calcite | Level 5
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

Tom
Super User Tom
Super User

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

 

b384b7
Calcite | Level 5

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?

Reeza
Super User

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?


 

b384b7
Calcite | Level 5

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

srinath3111
Quartz | Level 8

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; 

Satish_Parida
Lapis Lazuli | Level 10
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

Reeza
Super User

@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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 13 replies
  • 1707 views
  • 0 likes
  • 6 in conversation