BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
s_manoj
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

8 REPLIES 8
Kurt_Bremser
Super User

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.

Astounding
PROC Star

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;

Kurt_Bremser
Super User

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.

Astounding
PROC Star

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.

Ksharp
Super User

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;
s_manoj
Quartz | Level 8
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
Ksharp
Super User

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

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

PaigeMiller
Diamond | Level 26

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

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!

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
  • 8 replies
  • 3035 views
  • 7 likes
  • 5 in conversation