Monday, December 18, 2006

Making Indicator(dummy) variables

Following code can be used to make indicator variables.

Method 1

if state1 ne 20 & status ne 4 & sex ne .;
else if state1 in (1,3,4,19) then reg=1; /* Bread basket */
else if state1 in (8,16) then reg=2; /* maharashtraAP */
else if state1 in (9,10,12,13,17) then reg=3; /* bimaru states */
else if state1 in (5,11,21) then reg=5; /* north eastWB */
else if state1 in (2) then reg=6; /* Delhi */
else if state1 =. then reg=7; /* Missing */

ARRAY dummys {*} 3. reg_1 - reg_7;

DO i=1 TO 7;
dummys(i) = 0;
END;
dummys( reg ) = 1;

This is similar to the method given here.

Method 2

/* if variable values start from some arbitrary level */
/* following makes indicator for years */
/* Here year values can be from 1988 to 2000 */

ARRAY dummyb {1988:2000} 3. bat_1 - bat_13;

DO i=1988 TO 2000;
dummyb(i) = 0;
dummyb(i) =(year=i);
END;

Macros for creating indicator (dummy) variables are available from 1.
and SAS-L archives
Thanks for the comments Amy!

2 comments:

  1. I'd like to suggest an improvement:
    Because the categories of the variable REG are assigned on a mutually exclusive basis, use if-then-else coding rather than a series of "if" statements.

    1. It's more efficient, since once a condition is met and a value is assigned to REG, SAS will not uselessly check subsequent condition statements.

    2. It is readily apparent to the reader that the conditions are considered mutually exclusive.

    ReplyDelete