使用SAS的条件转置

时间:2019-05-07 20:02:03

标签: sas transpose lag

我有一个用于多个患者的数据集,如下所示。我正在尝试从相应变量的基线值中减去每个访问值(有时会丢失)。

Data Have: 
Patient Variable Value  Visit 
A       Height    100   Baseline
A       Weight     50   Baseline
A       HDCIRC     30   Baseline
A       BMI        50   Baseline
A       Height    100   a
A       Weight     50   a
A       HDCIRC     30   a
A       BMI        50   a
A       Height    100   b
A       Weight     50   b

Data Want: 
Patient Variable Value  Visit     BASELINE  Change 
A       Height    100   Baseline   100       0
A       Weight     50   Baseline   50        0
A       HDCIRC     30   Baseline   30        0
A       BMI        50   Baseline   50        0
A       Height    120   a          100       20
A       Weight     50   a          50        0
A       HDCIRC     30   a          30        0
A       BMI        34.7 a          50        -15.3
A       Height    150   b          100       50
A       Weight     51   b          50        1

我的尝试是先创建BASELINE,然后计算更改。 为了获得BASELINE,我见过有人使用滞后或dif函数。如何正确创建BASELINE变量?

proc sort data=have;
by patient visit;
;

data want;
set have;
by patient visit;
difstamp = dif(visit);
if first.patient then do;
  dif=0;
end;
else dif=difstamp;
drop difstamp;
run;

proc sort data=want;
by timestamp;
run;

2 个答案:

答案 0 :(得分:1)

按患者变量排序可能会有所帮助,以便您可以获取基线。 如果您的VISIT变量在第一次访问时未正确将BASELINE排序,则可以使用WHERE =数据集选项来确保基线首先出现。

data have;
input Patient $ Variable $ Value Visit $;
cards;
A       Height    100   Baseline
A       Weight     50   Baseline
A       HDCIRC     30   Baseline
A       BMI        50   Baseline
A       Height    120   a
A       Weight     50   a
A       HDCIRC     30   a
A       BMI        34.7 a
A       Height    150   b
A       Weight     51   b
;

proc sort;
 by patient variable visit;
run;
data want;
  set have(in=in1 where=(visit='Baseline'))
      have(in=in2 where=(visit^='Baseline'))
  ;
  by patient variable ;
  if first.variable then do;
    if in1 then baseline=Value;
    else baseline=.;
    retain baseline;
  end;
  if n(value,baseline)=2 then change=value-baseline;
run;

结果:

Obs    PATIENT    VARIABLE    VALUE    VISIT       BASELINE    CHANGE

   1       A        BMI         50.0    Baseline        50         0.0
   2       A        BMI         34.7    a               50       -15.3
   3       A        HDCIRC      30.0    Baseline        30         0.0
   4       A        HDCIRC      30.0    a               30         0.0
   5       A        Height     100.0    Baseline       100         0.0
   6       A        Height     120.0    a              100        20.0
   7       A        Height     150.0    b              100        50.0
   8       A        Weight      50.0    Baseline        50         0.0
   9       A        Weight      50.0    a               50         0.0
  10       A        Weight      51.0    b               50         1.0

答案 1 :(得分:1)

作为替代方案,您可以简单地将其自身合并

data have;
input Patient $ Variable $ Value Visit $;
cards;
A       Height    100   Baseline
A       Weight     50   Baseline
A       HDCIRC     30   Baseline
A       BMI        50   Baseline
A       Height    120   a
A       Weight     50   a
A       HDCIRC     30   a
A       BMI        34.7 a
A       Height    150   b
A       Weight     51   b
;

proc sort;
 by patient variable;
run;
data want;
  merge have have(where=(__visit='Baseline') keep=patient variable value visit rename=(visit=__visit value=BASELINE))
  ;
  by patient variable;
  Change=Value-BASELINE;
  drop __:;
run;