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

dear all:

I know how to do one-hot encoding with data step.Is it possible to do it by using proc transpose?

79b91ef3efbc40aea8e69ab30c870186.png

data basic_info;
input name $ sex $;
datalines;
A Female
B Male
C Female
D Female
E Male
;
run;

Any sugesstion is welcome!

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

You need to have a value which you can transpose.

data basic_info;
input name $ sex $;
val = 1;
datalines;
A Female
B Male
C Female
D Female
E Male
;

proc transpose data=basic_info out=want1 (drop=_name_);
by name;
var val;
id sex;
run;

data want;
merge
  basic_info
  want1
;
by name;
run;

Conversion of missing to 0 can be done manually in the MERGE step, or with PROC STDIZE.

View solution in original post

4 REPLIES 4
Ksharp
Super User
data basic_info;
input name $ sex $;
dummy=1;
datalines;
A Female
B Male
C Female
D Female
E Male
;
run;
proc glmselect data=basic_info outdesign(addinputvars)=want(drop=dummy);
class sex;
model dummy=sex/selection=none noint;
run;
duanzongran
Obsidian | Level 7
Thank you!
I didn't expect that there would be other processes that could also solve my problem.
Kurt_Bremser
Super User

You need to have a value which you can transpose.

data basic_info;
input name $ sex $;
val = 1;
datalines;
A Female
B Male
C Female
D Female
E Male
;

proc transpose data=basic_info out=want1 (drop=_name_);
by name;
var val;
id sex;
run;

data want;
merge
  basic_info
  want1
;
by name;
run;

Conversion of missing to 0 can be done manually in the MERGE step, or with PROC STDIZE.

Reeza
Super User
And just a note, though you may already be aware, that if using certain procedures this is unnecessary as param=ref will do the encoding automatically.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 4 replies
  • 618 views
  • 3 likes
  • 4 in conversation