在 SAS 9.4 中计算多行和多列的中位数

Calculating median across multiple rows and columns in SAS 9.4

我尝试搜索了多个地方,但还没有找到解决方案。我想知道这里是否有人可以帮助我?

我正在尝试计算 SAS 9.4 中多行和多列的中值(使用 Q1 和 Q3)我正在使用的数据集如下所示:

Obs tumor_size_1 tumor_size_2 tumor_size_3 tumor_size_4
1   4            1.5          1            1
2   2.5          2            .            .
3   3            .            .            .
4   4            .            .            .
5   3.5          1            .            .

上下文是针对一个人可能有 1 个(或多个)肿瘤的医疗状况。每行代表 1 个人。每个人最多可能有 4 个肿瘤。我想确定整个队列中所有肿瘤的中值大小(而不仅仅是每个人的中值大小)。有没有办法计算这个?提前谢谢你。

数据的转置将产生一个数据结构(形式),该数据结构(形式)适用于中位数和四分位数计算,在各种聚合组合中,由 PROC SUMMARYCLASS 语句组成。

示例:

data have;
input
patient tumor_size_1 tumor_size_2 tumor_size_3 tumor_size_4; datalines;
1   4            1.5          1            1
2   2.5          2            .            .
3   3            .            .            .
4   4            .            .            .
5   3.5          1            .            .
;

proc transpose data=have out=new_have;
by patient;
var tumor:;
run;

proc summary data=new_have;
  class patient;
  var col1;
  output out=want Q1=Q1 Q3=Q3 MEDIAN=MEDIAN N=N;
run;

结果

patient    _TYPE_    _FREQ_    Q1     Q3     MEDIAN     N

   .          0        20       1    3.50     2.25     10
   1          1         4       1    2.75     1.25      4
   2          1         4       2    2.50     2.25      2
   3          1         4       3    3.00     3.00      1
   4          1         4       4    4.00     4.00      1
   5          1         4       1    3.50     2.25      2

_TYPE_ 列描述了组合 CLASS 变量以获得所请求统计结果的方式。 _TYPE_ = 0 情况适用于所有值,并且在这个问题中,_FREQ_ = 20 表示 20 个输入进入计算考虑,其中 N = 10 是非缺失的并且涉及实际计算。当有多个CLASS变量时,_TYPE_的作用更加明显。

来自 Output Data Set 文档:

  • the variable _TYPE_ that contains information about the class variables. By default _TYPE_ is a numeric variable. If you specify CHARTYPE in the PROC statement, then _TYPE_ is a character variable. When you use more than 32 class variables, _TYPE_ is automatically a character variable.

The value of _TYPE_ indicates which combination of the class variables PROC MEANS uses to compute the statistics. The character value of _TYPE_ is a series of zeros and ones, where each value of one indicates an active class variable in the type. For example, with three class variables, PROC MEANS represents type 1 as 001, type 5 as 101, and so on.

一种不太优雅的计算所有值的中位数的方法是将所有值存储在一个超大数组中,并在读入最后一行后对数组使用 MEDIAN 函数:

data median_all;
  set have end=lastrow;
  array values [1000000] _temporary_;
  array sizes tumor_size_1-tumor_size_4;

  do sIndex = 1 to dim(sizes);
/*    if not missing (sizes[sIndex]) then do; */  %* decomment for dense fill;
      vIndex + 1;
      values[vIndex] = sizes[sIndex];
/*    end; */                                     %* decomment for dense fill;
  end;

  if lastrow then do;
    median_all_tumor_sizes = median (of values(*));
    output;
    put (median:) (=);
  end;

  keep median:;
run;

-------- LOG -------
median_all_tumor_sizes=2.25