使用SAS的条件转置

使用SAS的条件转置,sas,transpose,lag,Sas,Transpose,Lag,我有一个多个患者的数据集,如下所示。我试图从相应变量(有时缺失)的基线值中减去每个就诊值 我的尝试是首先创建基线,然后计算更改。 为了获得基线,我见过一些人使用滞后或dif函数。如何正确创建基线变量 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 d

我有一个多个患者的数据集,如下所示。我试图从相应变量(有时缺失)的基线值中减去每个就诊值

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

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;

按患者变量排序可能有助于您获得基线。 如果您的访问变量没有将基线正确地排序到第一次访问,那么您可以使用WHERE=dataset选项确保基线首先出现

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

按患者变量排序可能有助于您获得基线。 如果您的访问变量没有将基线正确地排序到第一次访问,那么您可以使用WHERE=dataset选项确保基线首先出现

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

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

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;

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

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;