如何使用权重在 SAS 中创建包含分位数排名组的列?

问题描述 投票:0回答:1

我有一个数据集,其中包含以下相关变量以及许多其他变量:

data have;
 input group $ score weight;
    datalines;
A 12 1.2
A 19 1
A 23 1.7
A 40 2
A 11 1
A 16 1.5
A 34 1.3
A 45 1
A 28 1.8
A 30 1.2
A 24 1.1
A 28 1
B 31 1.5
B 32 1.6
B 36 1.9
B 18 2
B 28 1.4
B 25 1.3
B 22 1.8
B 27 1.6
B 31 1.5
B 42 1.7
;
run;

我习惯在SAS中使用procrank语句进行排序组,例如:

proc rank data=have groups=4 out=want;
  by group;
  var score;
  ranks score_quartiles;
run;

但是 PROC RANK 不处理权重,对于我的研究,我需要使用 proc 单变量,转置结果,删除重复的关系,然后合并回原始数据集。但我不知道该怎么做。

非常感谢任何帮助。

sas weighted ranking-functions
1个回答
0
投票

与其尝试生成格式,不如直接从 PROC UNIVARIATE 的输出生成一些 IF/THEN 逻辑。

因此,首先使用 PROC UNIVARIATE 和 WEIGHT 语句来生成您想要用于排名的切点。

proc univariate data=have noprint;
  by group;
  var score;
  weight weight;
  output out=wide pctlpre=p_ pctlpts=25 to 75 by 25;
run;

proc transpose data=wide out=tall name=percentile ;
  by group;
  var p_: ;
run;

然后使用它们生成 IF/THEN 逻辑,根据 GROUP 和 SCORE 分配 RANK。

filename code temp;
data _null_;
  set tall;
  by group;
  file code;
  if first.group then do;
    rank=0;
    put 'if ' group= :$quote. 'then do;' 
      / '  if missing(score) then rank=.; '
    ;
  end;
  rank+1;
  put '  else if score < ' col1 'then ' rank= ';' ;
  if last.group then do;
    rank+1;
    put '  else ' rank= ';' / 'end;' ;
  end;
run;

然后运行代码

data want;
  set have;
%include code / source2;
run;

对于导致此数据步骤的示例数据:

833  data want;
834    set have;
835  %include code / source2;
NOTE: %INCLUDE (level 1) file CODE is file ...
836 +if group="A" then do;
837 +  if missing(score) then rank=.;
838 +  else if score < 19 then rank=1 ;
839 +  else if score < 28 then rank=2 ;
840 +  else if score < 34 then rank=3 ;
841 +  else rank=4 ;
842 +end;
843 +if group="B" then do;
844 +  if missing(score) then rank=.;
845 +  else if score < 25 then rank=1 ;
846 +  else if score < 31 then rank=2 ;
847 +  else if score < 32 then rank=3 ;
848 +  else rank=4 ;
849 +end;
NOTE: %INCLUDE (level 1) ending.
850  run;

NOTE: There were 22 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.WANT has 22 observations and 4 variables.
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.