Cornell University Cornell University CISER

CISER Computing

How to Create Dummy Variables in SAS

Say you have an age variable with five values: 1, 2, 3, 4 and 5, and you want to make five dummy variables of it.

DATA TEST;
INPUT age;
DATALINES;
1
2
3
4
5

;



Method 1

DATA DUMMYMETHOD1;
     SET TEST;
age1=(age=1);
age2=(age=2);
age3=(age=3);
age4=(age=4);
age5=(age=5);

PROC FREQ;
     TABLE age1 age2 age3 age4 age5;
RUN;

Note: The statement age1=(age=1) will create a variable called age1 with a value of 1 if age=1, and 0 if otherwise.



Method 2

DATA DUMMYMETHOD2 (DROP = i);
     SET TEST;
ARRAY A {*} age1 age2 age3 age4 age5;
DO i = 1 TO 5;
     A(i) = (age=i);
END;

PROC FREQ;
     TABLE age1 age2 age3 age4 age5;
RUN;

Note: An alternative representation to the ARRAY statement above is:
ARRAY A {*} age1-age5;
This alternative is useful if you are creating many dummy variables (assuming age has more than five unique values).



Method 3

DATA DUMMYMETHOD1;
     SET TEST;
IF age=1 then age1=1; ELSE age1 = 0;
IF age=2 then age2=1; ELSE age2 = 0;
IF age=3 then age3=1; ELSE age3 = 0;
IF age=4 then age4=1; ELSE age4 = 0;
IF age=5 then age5=1; ELSE age5 = 0;

PROC FREQ;
     TABLE age1 age2 age3 age4 age5;
RUN;

Note: This option is not useful for creating dummy variables out of a variable with many unique data values.