why this is wrong?
data age1;
set age.age;
if age < 55 then age2 = 1;
if age >=55 or age < 65 then age2 = 2;
if age >=65 or age <75 then age2 = 3;
if age >=75 or age < 85 then age2 = 4;
else age2 = 5;
run;
Your use of "or" as a logical operator causes this, combined with the lack of "else"s to prevent entering the other branches.
There are several solutions to your problem:
Use "and" instead of "or":
data age1;
set age;
if age < 55 then age2 = 1;
if age >= 55 and age < 65 then age2 = 2;
if age >= 65 and age < 75 then age2 = 3;
if age >= 75 and age < 85 then age2 = 4;
if age >= 85 then age2 = 5;
run;
Use "else":
data age1;
set age;
if age < 55 then age2 = 1;
else if age < 65 then age2 = 2;
else if age < 75 then age2 = 3;
else if age < 85 then age2 = 4;
else age2 = 5;
run;
Use a select block:
data age1;
set age;
select;
when (age < 55) age2 = 1;
when (age < 65) age2 = 2;
when (age < 75) age2 = 3;
when (age < 85) age2 = 4;
otherwise age2 = 5;
end;
run;
Use a format with ranges:
proc format;
value myage
low -< 55 = 1
55 -< 65 = 2
65 -< 75 = 3
75 -< 85 = 4
85 - high = 5
;
run;
data age1;
set age;
age2 = input(put(age,myage.),best.);
run;
The additional input() function is needed to make age2 numeric.
@SChander1 wrote:
why this is wrong?
data age1; set age.age; if age < 55 then age2 = 1; if age >=55 or age < 65 then age2 = 2; if age >=65 or age <75 then age2 = 3; if age >=75 or age < 85 then age2 = 4; else age2 = 5; run;
Just pick a number and see how the values of AGE2 change as the IF statements are evaluated.
So if AGE=30 then it is less than 50 so AGE2 is set to 1.
Then since it is also less than 65 it is set to 2.
etc.
So everything less than 85 will have AGE2 set to 4 and values equal to or greater than 85 will be set to 5.
Your use of "or" as a logical operator causes this, combined with the lack of "else"s to prevent entering the other branches.
There are several solutions to your problem:
Use "and" instead of "or":
data age1;
set age;
if age < 55 then age2 = 1;
if age >= 55 and age < 65 then age2 = 2;
if age >= 65 and age < 75 then age2 = 3;
if age >= 75 and age < 85 then age2 = 4;
if age >= 85 then age2 = 5;
run;
Use "else":
data age1;
set age;
if age < 55 then age2 = 1;
else if age < 65 then age2 = 2;
else if age < 75 then age2 = 3;
else if age < 85 then age2 = 4;
else age2 = 5;
run;
Use a select block:
data age1;
set age;
select;
when (age < 55) age2 = 1;
when (age < 65) age2 = 2;
when (age < 75) age2 = 3;
when (age < 85) age2 = 4;
otherwise age2 = 5;
end;
run;
Use a format with ranges:
proc format;
value myage
low -< 55 = 1
55 -< 65 = 2
65 -< 75 = 3
75 -< 85 = 4
85 - high = 5
;
run;
data age1;
set age;
age2 = input(put(age,myage.),best.);
run;
The additional input() function is needed to make age2 numeric.
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!
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.