Hello,
I have a dataset which consists of over 100 continuous variables.
What I want to do is to creat corresponding dummy variables in the same dataset according to their medians.
Because of the large number of variables, I'd like to do it by means of %macro.
In the attached, I first input a dataset as an example and provide some codes (syntax) that I've tried.
I have basic ability in programming, but not in coding macro syntax at all.
Could you please do me a favor? I apprecite your efforts and help.
I don't think you need a macro. What you really need is
Here's an example using sashelp.class:
proc means data=sashelp.class noprint ;
vars age -numeric- weight;
output out=meds (drop=_type_ _freq_) median= p50= / autoname;
run;
data want;
set sashelp.class ;
if _n_=1 then set meds;
array vars {*} age--weight ;
array meds {*} age_median--weight_median;
array p50 {*} age_p50--weight_p50;
do i=1 to dim(vars);
p50{i}=ifn(vars{i}=.,.,vars{i}>meds{i});
end;
drop age_median--weight_median i;
run;
What I want to do is to create corresponding dummy variables in the same dataset according to their medians.
What happens after you find the medians of each variable? What is the next step? How do you go from medians to dummy variables?
I doubt a macro is needed here, but the answer really depends on what you do with the medians.
I don't think you need a macro. What you really need is
Here's an example using sashelp.class:
proc means data=sashelp.class noprint ;
vars age -numeric- weight;
output out=meds (drop=_type_ _freq_) median= p50= / autoname;
run;
data want;
set sashelp.class ;
if _n_=1 then set meds;
array vars {*} age--weight ;
array meds {*} age_median--weight_median;
array p50 {*} age_p50--weight_p50;
do i=1 to dim(vars);
p50{i}=ifn(vars{i}=.,.,vars{i}>meds{i});
end;
drop age_median--weight_median i;
run;
Thank PaigeMiller and mkeintz for your prompt responses.
I can't believe I can get help immediately.
The example provide by mkeintz completely solved my question.
And it's so elegant. (although I still don't understand a part of the program, it works well.)
Thank yo so much for the kind help.
Wish you a Merry Christmas & Happy New Year !!
Cheers
Which part of my program that you don't understand yet? I can edit my answer to clarify.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.