Hi,
I am trying to recode the variable "income" (numeric) into 5 stratified categories (new stratified variable = ses). Although my output demonstrates that only one category ("Q5") is being coded.
Is there a problem with my if/then/else statements?
data data1;
set data0;
if income<25000 then ses='Q1';
if income<50000>=25000 then ses='Q2';
if income<80000>=50000 then ses='Q3';
if income<130000>=80000 then ses='Q4';
if income>=130000 then ses='Q5';
else sestatus=' ';
run;
This is how it should be coded.
data data1;
set data0;
if income < 25000 then ses='Q1';
else if 25000 < income <= 50000 then ses='Q2';
else if 50000 < income <= 80000 then ses='Q3';
else if 80000 < income < 130000 then ses='Q4';
else if income >= 130000 then ses='Q5';
else sestatus=' ';
run;
Note that if the income variable is missing it will get coded to Q1 with this logic. If you want to account for that you should do it as the first step.
data data1;
set data0;
if missing(income) then call missing(ses);
else if income < 25000 then ses='Q1';
else if 25000 < income <= 50000 then ses='Q2';
else if 50000 < income <= 80000 then ses='Q3';
else if 80000 < income < 130000 then ses='Q4';
else if income >= 130000 then ses='Q5';
else sestatus=' ';
run;
@njgrubic wrote:
Hi,
I am trying to recode the variable "income" (numeric) into 5 stratified categories (new stratified variable = ses). Although my output demonstrates that only one category ("Q5") is being coded.
Is there a problem with my if/then/else statements?
data data1;
set data0;
if income<25000 then ses='Q1';
if income<50000>=25000 then ses='Q2';
if income<80000>=50000 then ses='Q3';
if income<130000>=80000 then ses='Q4';
if income>=130000 then ses='Q5';
else sestatus=' ';run;
This is how it should be coded.
data data1;
set data0;
if income < 25000 then ses='Q1';
else if 25000 < income <= 50000 then ses='Q2';
else if 50000 < income <= 80000 then ses='Q3';
else if 80000 < income < 130000 then ses='Q4';
else if income >= 130000 then ses='Q5';
else sestatus=' ';
run;
Note that if the income variable is missing it will get coded to Q1 with this logic. If you want to account for that you should do it as the first step.
data data1;
set data0;
if missing(income) then call missing(ses);
else if income < 25000 then ses='Q1';
else if 25000 < income <= 50000 then ses='Q2';
else if 50000 < income <= 80000 then ses='Q3';
else if 80000 < income < 130000 then ses='Q4';
else if income >= 130000 then ses='Q5';
else sestatus=' ';
run;
@njgrubic wrote:
Hi,
I am trying to recode the variable "income" (numeric) into 5 stratified categories (new stratified variable = ses). Although my output demonstrates that only one category ("Q5") is being coded.
Is there a problem with my if/then/else statements?
data data1;
set data0;
if income<25000 then ses='Q1';
if income<50000>=25000 then ses='Q2';
if income<80000>=50000 then ses='Q3';
if income<130000>=80000 then ses='Q4';
if income>=130000 then ses='Q5';
else sestatus=' ';run;
The proper approach to issues like this is to create a value format with ranges, and assign it to the income variable when you use it as CLASS variable in analytic procedures.
Even when you create a new variable, using the format in a PUT statement makes for much cleaner code.
To visualize your problem, I added empty lines:
data data1;
set data0;
if income<25000 then ses='Q1';
if income<50000>=25000 then ses='Q2';
if income<80000>=50000 then ses='Q3';
if income<130000>=80000 then ses='Q4';
if income>=130000
then ses='Q5';
else sestatus=' ';
run;
The last IF-THEN-ELSE will code all incomes less than 130000 as missing.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.