BookmarkSubscribeRSS Feed
JChan10
Calcite | Level 5
Hi,

Instead of keep using the following programming;
if diag_flag = 'A04' then Diagflag_A04_Dummy = 1;
else Diagflag_A04_Dummy = 0;
if diag_flag = 'A41' then Diagflag_A41_Dummy = 1;
else Diagflag_A41_Dummy = 0;
if diag_flag = 'C16' then Diagflag_C16_Dummy = 1;
else Diagflag_C16_Dummy = 0;.....

How can I convert my data in column (Diagflag) to multiple Dummy variables,
Data currently looking like
Diagflag
D12
C13
Q15
D12
A14

Want outcome with cell value become column heading and 0,1 as values
D12 C13 Q15 A14.....
1 0 0 0
0 1 0 0
0 0 1 0
1 0 0 0
0 0 0 1
3 REPLIES 3
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
SAS PROC TRANSPOSE can accomodate your requirement.

Scott Barry
SBBWorks, Inc.
JChan10
Calcite | Level 5
I think the Transpose will put back the actual row observations back to the column but not the 0 and 1. I just want 0 and 1.

For example, under diagflag column, first obsevation is D12, then I would like to have a new column create named as D12 and the value in the first row under column D12 is 1.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Yes, you will need to carry a numeric variable to be used as an indicator, and use this variable by name in the PROC TRANSPOSE for the VAR statement along with the ID statement as well. Also code a PREFIX= to get the transposed columns having a defined prefix.

The key here is that you need to take the _N_ SAS-reserved variable (observation number) and assign it to a SAS numeric variable, say called OBSNUM. And this variable must be called-out in your ID statement so that you get one observation output for each transposed observation -- this approach maintains your vertical perspective.

What you will have generated is a new file, transposed with either a 1 or 0, depending on what was present in the input file.

Then, you will need to have a DATA step to loop through the transposed variables and if you find a missing value, change it to a zero -- that's better than setting MISSING='0' in my experience.

You'll find SAS-hosted product documentation and also supplemental technical and conference papers on various topics, like this one. I have provided a link at the bottom for one example on PROC TRANSPOSE.


Scott Barry
SBBWorks, Inc.

Learn the Basics of Proc Transpose
Douglas Zirbel, Wells Fargo and Co., St. Louis, Missouri
http://support.sas.com/resources/papers/proceedings09/060-2009.pdf

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 862 views
  • 0 likes
  • 2 in conversation