BookmarkSubscribeRSS Feed
H
Pyrite | Level 9 H
Pyrite | Level 9

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.

5 REPLIES 5
Tom
Super User Tom
Super User

Look at PROC GLMMOD.

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

Ksharp
Super User

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

H
Pyrite | Level 9 H
Pyrite | Level 9

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:

DATA New;

     SET Old;

     ARRAY dummys {*} 3. Cat_Var_1 - Cat_Var_4;

     DO i=1 TO 4;

     dummys(i)=0;

   END;

     dummys(Cat_Var) = 1;

     DROP i;

RUN;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

arame
Calcite | Level 5

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 ;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 8641 views
  • 0 likes
  • 5 in conversation