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: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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