BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
njgrubic
Fluorite | Level 6

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;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;

 

 


 

View solution in original post

5 REPLIES 5
Reeza
Super User

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;

 

 


 

njgrubic
Fluorite | Level 6
Thanks reeza,
I ended up adding in 0 to the "Q1" classificaiton to ensure I was not including 'blank' observations.

if 0 < 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 ses=' ';
Reeza
Super User
Are you sure all income must be positive?
Kurt_Bremser
Super User

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.

Kurt_Bremser
Super User

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 5 replies
  • 481 views
  • 2 likes
  • 3 in conversation