Dear All,
I want to use apply a same procedure on each variable. For example,
1. rename old variable sex age to sex_new sex_old;
2. proc rank each variable and fit model using ranks;
so on so forth.
I tried array and macro but just can't get it work........
Thanks a lot in advance!
Best wishes,
Hi:
This documentation example shows using GROUPS= with PROC RANK:
http://support.sas.com/documentation/cdl/en/proc/68954/HTML/default/viewer.htm#p1xzpoijq32wbsn1gr6g5... (Example 3)
And assuming that you want all the numeric variables to be ranked, you can modify this example to make a macro variable to hold the names of all the numeric variables and then a second macro variable to hold the names of the "rank_" varible to be created.
Here's the code (the PROC SQL step is the modification from the documentation):
options nodate pageno=1 linesize=80 pagesize=60;
data swim;
input Name $ 1-7 Gender $ 9 Back 11-14 Free 16-19;
datalines;
Andrea F 28.6 30.3
Carole F 32.9 24.0
Clayton M 27.0 21.9
Curtis M 29.0 22.6
Doug M 27.3 22.4
Ellen F 27.8 27.0
Jan F 31.3 31.2
Jimmy M 26.3 22.5
Karin F 34.6 26.2
Mick M 29.0 25.4
Richard M 29.7 30.2
Sam M 27.2 24.1
Susan F 35.1 36.1
;
run;
proc sql;
select name, catx('_','rank',name) into :vname separated by ' ',
:rname separated by ' '
from dictionary.columns
where (libname='WORK' and memname = 'SWIM') and
type = 'num';
quit;
%put vname is &vname;
%put rname is &rname;
proc sort data=swim out=pairs;
by gender;
run;
proc rank data=pairs out=rankpair groups=3;
by gender;
var &vname;
ranks &rname;
run;
proc print data=rankpair n;
by gender;
title 'Pairings of Swimmers for Backstroke and Freestyle';
run;
And here are the results:
cynthia
Please show the code of what you tried.
You are not going to be able to rename using array, that is either a rename statement or use proc datasets with a modify section for that dataset.
What type of rank? On which variables? What kind of model? What dependent variable(s)? What independent variables?
Sorry i should have make my queations more clearly. Say I have the following data set:
grade age
10 11
5 6
15 20
8 9
I want to use proc rank to rank the two variables in five groups:
proc rank groups=5 out=rank data=data;
var _ALL_;
ranks grade_rank age_rank;
run;
But since i have hundreds of variables, i cannot name every new names in 'ranks'. And i want the new coloumn names contains the original variable name then end or begins with 'rank'.
I'm thinking about using array or macro, but it doesn't work. I don't have that codes anymore, i deleted them already.....
The reason why i mentioned rename is that, now i'm using code:
proc rank groups=5 out=rank data=data;
var _ALL_;
ranks rank1-rank100;
run;
And i tried to rename rank1 - rank 100 to the name i desired. I cannot get this done neither........
Thanks again!!
Best,
Hi:
This documentation example shows using GROUPS= with PROC RANK:
http://support.sas.com/documentation/cdl/en/proc/68954/HTML/default/viewer.htm#p1xzpoijq32wbsn1gr6g5... (Example 3)
And assuming that you want all the numeric variables to be ranked, you can modify this example to make a macro variable to hold the names of all the numeric variables and then a second macro variable to hold the names of the "rank_" varible to be created.
Here's the code (the PROC SQL step is the modification from the documentation):
options nodate pageno=1 linesize=80 pagesize=60;
data swim;
input Name $ 1-7 Gender $ 9 Back 11-14 Free 16-19;
datalines;
Andrea F 28.6 30.3
Carole F 32.9 24.0
Clayton M 27.0 21.9
Curtis M 29.0 22.6
Doug M 27.3 22.4
Ellen F 27.8 27.0
Jan F 31.3 31.2
Jimmy M 26.3 22.5
Karin F 34.6 26.2
Mick M 29.0 25.4
Richard M 29.7 30.2
Sam M 27.2 24.1
Susan F 35.1 36.1
;
run;
proc sql;
select name, catx('_','rank',name) into :vname separated by ' ',
:rname separated by ' '
from dictionary.columns
where (libname='WORK' and memname = 'SWIM') and
type = 'num';
quit;
%put vname is &vname;
%put rname is &rname;
proc sort data=swim out=pairs;
by gender;
run;
proc rank data=pairs out=rankpair groups=3;
by gender;
var &vname;
ranks &rname;
run;
proc print data=rankpair n;
by gender;
title 'Pairings of Swimmers for Backstroke and Freestyle';
run;
And here are the results:
cynthia
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.