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?
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;
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With