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?
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
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
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
Thanks, @PaigeMiller
I am using the dummies for sum but great to know that SAS will handle the problem.
@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.
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.
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.