How to create several dummy variable when category variable is characteristic?

Reply
Occasional Contributor
Posts: 14

How to create several dummy variable when category variable is characteristic?

[ Edited ]

Hello there.

 

I just wonder if there is a better way to write the following codes (to create a set of dummy variables)

I'm trying to use array and then I find out that I still have to write all of those "if.." sentences


data test;
input Category;
cards;
A
B
C
D
E
D
E
D
G
A
;
run;

run;

data test;
set test;
if drug = "A" then Z1=1; else Z1=0;
if drug = "B" then Z2=1; else Z2=0;
if drug = "C" then Z3=1; else Z3=0;
(...)

 

Thanks so much!

 

Super User
Posts: 5,256

Re: How to create several dummy variable when category variable is characteristic?

The output layout itself look awkward. Any specific requirement for this?
Any solutions where you expand the no of variables will result in awkward programs and data management.
Data never sleeps
Occasional Contributor
Posts: 14

Re: How to create several dummy variable when category variable is characteristic?

Thanks for replying LinusH.

That's just a question in my homework, and it is fine if I just write several "if.." sentences.

However, I want to practice my coding skills (You can probably tell that I'm just a beginner) and that's how I came up with this.

Super User
Posts: 17,829

Re: How to create several dummy variable when category variable is characteristic?

1. Make sure you need to. Many procs in SAS use a CLASS statement that account for categorical variables.

2. Search on here - I think there's quite a few good idea's on here, from Rick Wicklin & PGStats in particular.

Occasional Contributor
Posts: 14

Re: How to create several dummy variable when category variable is characteristic?

Thanks Reeza, 

 

I wonder if you can tell me more about Rick Wicklin & PGStats? Where can I find their work?

Super User
Posts: 17,829

Re: How to create several dummy variable when category variable is characteristic?


joseph626 wrote:

Thanks Reeza, 

 

I wonder if you can tell me more about Rick Wicklin & PGStats? Where can I find their work?


On here...use the search bar at the top of the page. 

Once you're on the search page you can limit it to their posts. I like the GLMMOD solution here:

http://communities.sas.com/t5/Base-SAS-Programming/Dummy-variables-for-years/m-p/238838/highlight/tr...

 

Super User
Posts: 10,500

Re: How to create several dummy variable when category variable is characteristic?

[ Edited ]

Maybe not that much easier:

 

Z1= (category="A"); (your Test set does not have a variable named drug)

 

Or

data want;
   set test;

   array z {*} z1 - z6 ;
   do i = 1 to dim(z);
      z[i]=0;
   end;
   i=whichc(category,'A','B','C','D','E','G');
   Z[I]=1;
   drop i;
RUN;

 

 

Occasional Contributor
Posts: 14

Re: How to create several dummy variable when category variable is characteristic?

Thanks Ballardw!

 

That's actually much better than my of "if..."s, lol!

Super User
Posts: 9,681

Re: How to create several dummy variable when category variable is characteristic?

You are trying to get design matrix . Check this :
http://blogs.sas.com/content/iml/2016/02/24/create-a-design-matrix-in-sas.html




data test;
input Category $;
cards;
A
B
C
D
E
D
E
D
G
A
;
run;
data Temp / view=Temp;
   set test;
   FakeY = 0;
run;
 
proc logistic data=Temp outdesign=Want_EffectDesign(drop=FakeY) outdesignonly;
   class Category / param=glm; 
   model FakeY =Category;
run;


Ask a Question
Discussion stats
  • 8 replies
  • 412 views
  • 1 like
  • 5 in conversation