Skip to content

Instantly share code, notes, and snippets.

@statgeek
Last active February 24, 2018 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save statgeek/7cffd06ebc3bc9c78b4f6a5b4538b053 to your computer and use it in GitHub Desktop.
Save statgeek/7cffd06ebc3bc9c78b4f6a5b4538b053 to your computer and use it in GitHub Desktop.
SAS Proc Rank - automate ranking prefix
/*This macro will take a list of variables (varList)
and create an output dataset with a new variable for
each variable in the list that has the PREFIX specified
for the ranks. The example at the end can be run to
see the output generated
Author: F.Khurshed
Date: 2018-02-24
*/
%macro rank_prefix(data_in = , data_out= , varList= , prefix= , n_groups =);
%*Create rename list with prefix;
data _null_;
length new_v_list $100.;
string = "&varList";
n_words = countw(string);
do i=1 to n_words;
var = scan(string, i);
rankVar = catt("&prefix", var);
if length(rankVar) > 32 then do;
put 'Variable Name too long!!!';
end;
new_v_list = catx(" ", new_v_list, rankVar);
end;
call symputx('newVarList', new_v_list);
run;
proc rank data=&data_in. out=&data_out. groups = &n_groups.;
var &varList.;
ranks &newVarList.;
run;
%mend;
%rank_prefix(data_in =sashelp.class , data_out=want , varList= height weight , prefix=rank , n_groups =3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment