BookmarkSubscribeRSS Feed
HALOBEAST999
Fluorite | Level 6

I am trying to print an index between 1 and 4 for different conditions but my code is not working. Here is my code:

DATA cars.inx;
set Cars1;
Where Size=SMALL; DO;
If (0<Mileage<=20 & 1<=Reliable<=3) then index=1;
else if (0<Mileage<=20 & 4<=Reliable<=5) then index=2;
else if (21<=Mileage<=50 & 1<=Reliable<=3) then index=3;
else if (21<=Mileage<=50 & 4<=Reliable<=5) then index=4;
end;
Where Size = COMPACT; DO;
If (0<Mileage<=15 & 1<=Reliable<=3) then index=1;
else if (0<Mileage<=15 & 4<=Reliable<=5) then index=2;
else if (16<=Mileage<=50 & 1<=Reliable<=3) then index=3;
else if (16<=Mileage<=50 & 4<=Reliable<=5) then index=4;
end;
Where Size= MID-SIZED; DO;
If (0<Mileage<=12 & 1<=Reliable<=3) then index=1;
else if (0<Mileage<=12 & 4<=Reliable<=5) then index=2;
else if (13<=Mileage<=50 & 1<=Reliable<=3) then index=3;
else if (13<=Mileage<=50 & 4<=Reliable<=5) then index=4;
end;
proc print; run;

 

Anyone see where I am going wrong?

5 REPLIES 5
novinosrin
Tourmaline | Level 20

Hi @HALOBEAST999   Welcome to SAS communities.

 

Try the correction below and see if this works

 

DATA cars.inx;
set Cars1;
if Size='SMALL' then DO;
If (0<Mileage<=20 & 1<=Reliable<=3) then index=1;
else if (0<Mileage<=20 & 4<=Reliable<=5) then index=2;
else if (21<=Mileage<=50 & 1<=Reliable<=3) then index=3;
else if (21<=Mileage<=50 & 4<=Reliable<=5) then index=4;
end;
else if Size = 'COMPACT' then DO;
If (0<Mileage<=15 & 1<=Reliable<=3) then index=1;
else if (0<Mileage<=15 & 4<=Reliable<=5) then index=2;
else if (16<=Mileage<=50 & 1<=Reliable<=3) then index=3;
else if (16<=Mileage<=50 & 4<=Reliable<=5) then index=4;
end;
else if  Size= 'MID-SIZED' then DO;
If (0<Mileage<=12 & 1<=Reliable<=3) then index=1;
else if (0<Mileage<=12 & 4<=Reliable<=5) then index=2;
else if (13<=Mileage<=50 & 1<=Reliable<=3) then index=3;
else if (13<=Mileage<=50 & 4<=Reliable<=5) then index=4;
end;
run;
proc print; run;
maguiremq
SAS Super FREQ

You should post your data as well. By the looks of it, you should use if-then-do statements instead of where.

 

Where size=small; do --> "if size = 'small' then do"; then "else if size = 'compact' then do;" 

 

ChrisNZ
Tourmaline | Level 20

As mentioned, you are using the WHERE statement wrongly.

 

Read the documentation:

"The WHERE statement selects observations in SAS data sets only"

 

See https://documentation.sas.com/?docsetId=lestmtsref&docsetTarget=n1xbr9r0s9veq0n137iftzxq4g7e.htm&doc...

 

Here, you have 3 where clauses, so the latest clause is applied when reading the data.

s_lassen
Meteorite | Level 14

The code that @novinosrin gave you should work fine, but it seems to me that there is a lot of code duplication. Maybe you should try something like this (not tested, as you did not provide any data):

proc format;
  invalue mileage
    'SMALL'=20
    'COMPACT'=15
    'MID-SIZED'=12
    ;
run;

data cars.inx;
  set cars;
  _mileage_=input(size,mileage.);
  If (0<Mileage<=_mileage_ & 1<=Reliable<=3) then index=1;
  else if (0<Mileage<=_mileage_ & 4<=Reliable<=5) then index=2;
  else if (_mileage_+1<=Mileage<=50 & 1<=Reliable<=3) then index=3;
  else if (_mileage_+1<=Mileage<=50 & 4<=Reliable<=5) then index=4;
  drop _mileage_;
run;

 

FreelanceReinh
Jade | Level 19

Hi @HALOBEAST999,

 

I like @s_lassen's idea of making the code shorter. Depending on the data you might even go a step further in that direction:

data cars.inx(drop=_s);
set cars1;
_s=whichc(size,'SMALL','COMPACT','MID-SIZED');
if 0<mileage<=50 & 1<=reliable<=5 & _s then index=2*(mileage>choosen(_s,20,15,12))+(reliable>3)+1;
run;

This assumes integer values for mileage and reliable, but this is strongly suggested by your IF conditions anyway.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1071 views
  • 0 likes
  • 6 in conversation