Programming the statistical procedures from SAS

Creating Dummy Variable

Frequent Contributor
Frequent Contributor
Posts: 109

Creating Dummy Variable

I have a categorical variable, I will call it City. Each City has a number between 1-1500, though only 75 actual numbers are used between this range, with no pattern.  I want to create a dummy variable City_1 - City_75 for the 75 cities actually in the dataset.

Example data and dummy coded variable:

Current:                              Desired:

City = 1                              City_1 = 1

City = 1                              City_1 = 1

City = 12                            City_2 = 1

City = 44                            City_3 = 1

City = 163                          City_4 = 1

City = 163                          City_4 = 1

City = 199                          City_5 = 1

So implicitly if City_1 = 1, then City_2,...,City_5 would all = 0 for that observation (person).

The dummy code should be a numeric value.

Thanks in advance, and please ask questions if I did not provide enough information.

Super User
Super User
Posts: 6,163

Re: Creating Dummy Variable


Do you really need the dummy variables?  Most SAS proc will automatically create them for you if you use a CLASS statement.

Grand Advisor
Posts: 9,467

Re: Creating Dummy Variable

IML 's function DESIGN( ) .  OR  data step's  proc transpose.

Frequent Contributor
Frequent Contributor
Posts: 109

Re: Creating Dummy Variable

Thanks for the input.  I ended up not recoding the variable with all of the groups (not in sequential integer order). I just recoded another variable using the following code:


     SET Old;

     ARRAY dummys {*} 3. Cat_Var_1 - Cat_Var_4;

     DO i=1 TO 4;



     dummys(Cat_Var) = 1;

     DROP i;


Esteemed Advisor
Esteemed Advisor
Posts: 7,015

Re: Creating Dummy Variable

Sorry, I am still not seeing why you need the variables.  In your first example, if you create 1500 empty variables, and then fill only 75 you are both wasting storage space and making further processing more difficult.  Also, by group processing and class processing is far quicker than multiple calls with different variables.  Just create a categorising variable and use by group processing:

your_variables     city [The grouping var]

...                             CITY5

...                             CITY205

...                              CITY1000

Then you only have rows where needed and can do:

proc xyz;

     group by city.

Frequent Learner
Posts: 1

Re: Creating Dummy Variable

I think you should recode your variable like this :

if City = 44  then  City = 3

else if City = 163     City = 4 etc.

after that you can use the proc transreg :

proc transreg data=a(keep = city) noprint design  ;

    model class (city /zero=none) ;

    output out=a_dummicode(drop=_name_ _type_ intercept) ;

run ;

Ask a Question
Discussion stats
  • 5 replies
  • 5 in conversation