假设我有一个包含n行和p列的数据集,这样数据集中的每个条目都包含一个实数.我正在寻找一种方法来排列p每行中的列.此排名的输出应该是p考虑关系的排名长度.
所以,假设我的数据集有5列.第一行可能是这样的row 1 = {10, 13, 3, 3, -4}.我想在这一行上执行一些操作,最后得到结果row 1 ranks = {3, 4, 2, 2, 1}.第二行可能是类似row 2 = {8, 3, -6, 5, 2}的,此行的结果应该是row 2 ranks = {5, 3, 1, 4, 2}.
这个功能是在SAS中实现的吗?我已经生成了不考虑关系的代码,但是它们经常发生以至于需要花费不合理的时间来纠正错误地完成的行排名.
有趣的问题; 这是一个可能的解决方案:
data have;
p1=10; p2=13; p3=3; p4=3; p5=-4; output;
p1=8; p2=3; p3=-6; p4=5; p5=2; output;
run;
data want;
set have;
array p(*) p1-p5;
array c(*) c1-c5;
array r(*) r1-r5;
/* Copy vector to temp array and sort */
do i=1 to dim(p);
c(i) = p(i);
end;
call sortn(of c(*));
/* Search new sorted array for the original position */
do i=1 to dim(c);
if i = 1 then rank=1;
else if c(i) ne c(i-1) then rank + 1;
do j=1 to dim(p);
if p(j) = c(i) then do;
r(j) = rank;
end;
end;
end;
/* PUT statement to see result in log */
put +3 p(*)
/ +3 c(*)
/ +3 r(*);
drop i j rank c1-c5;
run;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3714 次 |
| 最近记录: |