BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dapenDaniel
Obsidian | Level 7

Hello,

 

I have a list of product name like below.

data have;
  input name $;
cards;
A1
A2
A5
B9
C2
C8;
run;

 

 

 

 

I would like to create dummies for each of them. The data I want is as below.

 

Name I1 I2 I3 I4 I5 I6

A1 1 0 0 0 0 0

A2 0 1 0 0 0 0

A5 0 0 1 0 0 0

B9 0 0 0 1 0 0

C2 0 0 0 0 1 0

C8 0 0 0 0 0 1

 

what code do I need to use?

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Here is one way. 

 

Inspired by this article by @Rick_SAS 

 

data have;
  input name $;
cards;
A1
A2
A5
B9
C2
C8
;
run;

data temp / view=temp;
   set have;
   FakeY = 0;
run;

proc glmmod data=temp outdesign=design(drop=FakeY Col1) noprint;
   class name;
   model FakeY = name;
run;

data want;
   merge have design;
run;

Result:

 

name Col2 Col3 Col4 Col5 Col6 Col7 
A1   1    0    0    0    0    0 
A2   0    1    0    0    0    0 
A5   0    0    1    0    0    0 
B9   0    0    0    1    0    0 
C2   0    0    0    0    1    0 
C8   0    0    0    0    0    1 

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

Here is one way. 

 

Inspired by this article by @Rick_SAS 

 

data have;
  input name $;
cards;
A1
A2
A5
B9
C2
C8
;
run;

data temp / view=temp;
   set have;
   FakeY = 0;
run;

proc glmmod data=temp outdesign=design(drop=FakeY Col1) noprint;
   class name;
   model FakeY = name;
run;

data want;
   merge have design;
run;

Result:

 

name Col2 Col3 Col4 Col5 Col6 Col7 
A1   1    0    0    0    0    0 
A2   0    1    0    0    0    0 
A5   0    0    1    0    0    0 
B9   0    0    0    1    0    0 
C2   0    0    0    0    1    0 
C8   0    0    0    0    0    1 
PaigeMiller
Diamond | Level 26

Generally, creating your own dummy variables is unnecessary as most SAS modelling procedures handle character variables via the CLASS statement, and then the procedure creates the dummy variables so you don't have to

 

--
Paige Miller
dapenDaniel
Obsidian | Level 7

Thanks, @PaigeMiller 

 

I am using the dummies for sum but great to know that SAS will handle the problem.

PaigeMiller
Diamond | Level 26

@dapenDaniel wrote:

Thanks, @PaigeMiller 

 

I am using the dummies for sum but great to know that SAS will handle the problem.


PROC FREQ will tell you how many times each one appears.

--
Paige Miller

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 672 views
  • 3 likes
  • 3 in conversation