Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dummy variables in SAS

Tags:

sas

Suppose we have some data set people which has a categorical variable income with 4 levels (1,2,3,4). How would we code this in SAS? Would it be:

data people;
set people;
if income=1 then income1=1;
else if income=2 then income2=1
else if income  =3 then income3=1;
run;

In other words, this would create three dummy variable for the four levels. Is this right?

like image 378
Damien Avatar asked Jan 11 '23 07:01

Damien


2 Answers

A somewhat more flexible way to do it is with arrays.

data people;
set people;
array incomes income1-income4;
do _t = 1 to dim(incomes);
  if income=_t then income[_t] = 1;
  else if not missing(income) then income[_t]=0;
  else income[_t]=.;
end;
run;
like image 186
Joe Avatar answered Jan 25 '23 07:01

Joe


I have modified your code below. This would give a 3 dummy coded variable. income = 4 would be your reference code.

data people_dummy;
         set people;
         if income=1 then income1=1 ; else income1=0;
         if income=2 then income2=1 ; else income2=0; 
         if income=3 then income3=1 ; else income3=0;
run;
like image 35
forecaster Avatar answered Jan 25 '23 08:01

forecaster