DATA Step, Macro, Functions and more

how to create a dummy variable for an existing variable

Accepted Solution Solved
Reply
Super Contributor
Posts: 413
Accepted Solution

how to create a dummy variable for an existing variable

[ Edited ]

Hi,

 

I have a data with over 20 variables and over a 100 rows of data.

 

For each variable I would like to create a dummy variable which depends on the median of the values of that variable. Here is an example to illustrate:

 

I have a column named "age" containing ages. I would like to calculate the median age, and suppose it equals 29, so if for an entry the value for age is greater than the median, say 35, then the new variable "age_dummy" will be equal to 1, and if age is less than or equal to the median, like 25, than the dummy will be equal to 0.

 

so if the initial data is something like this:

age
25
29
31
35

 

Then I would like the final data to be of the following:

age age_dummy
25 0
29 0
31 1
35 1

 

 

Thank you!


Accepted Solutions
Solution
‎09-11-2016 11:59 PM
Respected Advisor
Posts: 4,646

Re: how to create a dummy variable for an existing variable

[ Edited ]

Use the RANK procedure

 

proc rank data=have out=want groups=2;
var var1-var20;
ranks rvar1-rvar20;
run;

Below and above median will be identified with group number 0 and 1, respectively.

PG

View solution in original post


All Replies
Super User
Posts: 17,826

Re: how to create a dummy variable for an existing variable

So which part is your question? 

Super Contributor
Posts: 413

Re: how to create a dummy variable for an existing variable

Hi Reeza,

 

for each column I would like to calculate the median of the numbers, and then for each colum create a new column which will consist of dummy values (0 or 1). So basically there are 2 subquestions: calculating the median of a column and creating a new column of dummy variables.

 

Thanks!

Super Contributor
Posts: 413

Re: how to create a dummy variable for an existing variable

Here I would like to add some illustration:

 

suppose the initial data is something like this:

age
25
29
31
35

 

Then I would like the final data to be of the following:

age age_dummy
25 0
29 0
31 1
35 1

 

And please note that I have over 20 variables so if possible to make the entire process automatic for all the variables.

 

Thanks!

Solution
‎09-11-2016 11:59 PM
Respected Advisor
Posts: 4,646

Re: how to create a dummy variable for an existing variable

[ Edited ]

Use the RANK procedure

 

proc rank data=have out=want groups=2;
var var1-var20;
ranks rvar1-rvar20;
run;

Below and above median will be identified with group number 0 and 1, respectively.

PG
Super Contributor
Posts: 413

Re: how to create a dummy variable for an existing variable

Hi PGStats,

 

thank you for the reply, I clearly got the dummy values depending on the median.

 

now I get the dummy columns but the original columsn disappear, and I need the original columns and I can't just merge becasue both the original columns and their corresponding new dummy columns have the same name.

 

Thanks!

Super User
Posts: 17,826

Re: how to create a dummy variable for an existing variable

If you used the RANK statement that's not what should have happened. 

 

Please post your code. 

Also verify that the variable name and labels are different. 

 

http://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#a002473611.htm

Super Contributor
Posts: 413

Re: how to create a dummy variable for an existing variable

That's good now I realized that I forgot to put the "r" before the original varible name 

Respected Advisor
Posts: 4,646

Re: how to create a dummy variable for an existing variable

Dataset want should contain original variables (var1-var20) and new variables rvar1-rvar20, just as you need. I would need to see your log to understand what happened.

PG
Super Contributor
Posts: 413

Re: how to create a dummy variable for an existing variable

That's good now I realized that I forgot to put the "r" before the original varible name!

Valued Guide
Posts: 505

Re: how to create a dummy variable for an existing variable

 

I posted what I think is a more comprehensive 'R' based solution. This solution should map easly to IML.

 

https://listserv.uga.edu/cgi-bin/wa?A2=SAS-L;a2483999.1609b

Super Contributor
Posts: 413

Re: how to create a dummy variable for an existing variable

Hi rogerjdeangelis,

 

I would love to see the solution, but when I clicked on the link I was asked for a password, so is there another way of accessing your solution?

 

Thnak you!

☑ This topic is SOLVED.

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

Discussion stats
  • 11 replies
  • 657 views
  • 3 likes
  • 4 in conversation