简体   繁体   中英

SAS Proc Rank/Proc SQL

I have a dataset similar to

data NATR332;
input Y1 Y2;
datalines;
146 141
141 143
135 139
142 139
140 140
143 141
138 138
137 140
142 142
136 138
run;`

I used proc sql to find the difference between Y1 and Y2 and removed the rows where the difference is = 0 by using the code

proc SQL;
/*create table temp as*/
select *,
Y1 - Y2 as Difference
from NATR332
where (Y1-Y2 ^= 0)
;

I now want to create a new column called rank where I rank the absolute value of the differences. I tried to use the rank () over partition in proc sql and didn't have any luck so I was thinking I would maybe have to use the proc rank function. How would I go about creating this column? I am much more familiar with sql than I am sas so I try to do most of my work in proc sql when using sas.

Thank you in advance.

I would do the following:

data diffs;
set NATR332;
difference = abs(Y1-Y2);
if difference ne 0;
run;

proc rank data=diffs descending out=diffs_ranked;
var difference;
ranks ranking;
run;

You have a dataset called diffs_ranked and a variable called ranking that holds the ranks, largest to smallest because of the descending option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM