DATA Step, Macro, Functions and more

create dummy variables - Categorical Variables

Accepted Solution Solved
Reply
Contributor
Posts: 50
Accepted Solution

create dummy variables - Categorical Variables

[ Edited ]

Hi all,

I have dataset "SAMPLE", contains a player variable with records playerA, playerB,......... PlayerH, and country variable with records countryA, countryB,.....countryF. I want to create dummy variables for each category in player and country variables. If I go by writing general IF-THEN statement it takes very long code to write. Please suggest me very simple code to create dummy variables for player, country variable and also i want to create interaction variable between player and country variable.

 

Here sample dataset:

 

code.JPG

 Expected output:

 

code1.JPG

Thanks in advance

 

Regards

S Manoj


Accepted Solutions
Solution
‎03-07-2018 03:35 AM
Super User
Posts: 10,850

Re: create dummy variables - Categorical Variables

As other said ,post SAS CODE ,not picture, nobody would like to type it for you.

 

It is very easy for IML code. But here is BASE code. 

data new;
input score player $ country $;
dummy=1;
cards;
10 A A
28 B B
87 C B
;
run;
proc logistic data=new outdesignonly outdesign=design;
class player country/param=glm;
model dummy=player country;
run;
data want;
 merge new(drop=dummy) design(drop=dummy intercept);
run;
proc print noobs;run;

View solution in original post


All Replies
Super User
Posts: 10,588

Re: create dummy variables - Categorical Variables

Please supply example data in a data step with datalines, so we have data to work with.

And show the expected result for that example dataset, so we can get a picture for the logical rule behind it.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 6,935

Re: create dummy variables - Categorical Variables

It's quite difficult to picture with the final result should look like.  Here's one possibility.  Perhaps you could comment about what you want, vs. what this does:

 

proc sql;

create table want as select * from sample, country;

quit;

Super User
Posts: 10,588

Re: create dummy variables - Categorical Variables

Code is pure text. Please DO NOT POST PICTURES OF CODE!!! Forcing people to type text from pictures is kind of rude.

Use the proper buttons ("little running man" and {i}) to post code and logs or other fixed-space text.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 6,935

Re: create dummy variables - Categorical Variables

Before you go too far down this path, you should realize that SAS procedures often create both dummy variables and interaction terms as needed.  See if the procedure you intend to use supports the CLASS statement.

 

You run the risk of many complications here, including country names that contain multiple words, and player names that are too long to turn into a variable name.

Solution
‎03-07-2018 03:35 AM
Super User
Posts: 10,850

Re: create dummy variables - Categorical Variables

As other said ,post SAS CODE ,not picture, nobody would like to type it for you.

 

It is very easy for IML code. But here is BASE code. 

data new;
input score player $ country $;
dummy=1;
cards;
10 A A
28 B B
87 C B
;
run;
proc logistic data=new outdesignonly outdesign=design;
class player country/param=glm;
model dummy=player country;
run;
data want;
 merge new(drop=dummy) design(drop=dummy intercept);
run;
proc print noobs;run;
Contributor
Posts: 50

Re: create dummy variables - Categorical Variables

Thank you for code,
and I just want to know, within in this code is there any way to get the output with customized dummy variable name
Super User
Posts: 10,850

Re: create dummy variables - Categorical Variables

No. you can't. But you change variable name after getting DESIGN dataset.

If you are using IML code, IML can do that.

Respected Advisor
Posts: 3,279

Re: create dummy variables - Categorical Variables

Very long thread discussing many ways to create dummy variables

https://communities.sas.com/t5/SAS-Procedures/How-can-i-create-dummy-variables-How-can-i-change-my-c...

 

Furthermore, if you are one the many modeling procedures in SAS, you don't need to create your own dummy variables anyway, this is all done internally in the procedure.

--
Paige Miller
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 225 views
  • 7 likes
  • 5 in conversation