为SAS中的所有观测值保留值

时间:2019-07-15 02:53:22

标签: sas

我的数据集如下所示,但有多个患者:

ID   Variable   val    Visit  
A     Height     5     Base
A     Weight     3     Base
A     BMI        1     Base
A     Height     2    Visit 1
A     Weight     4    Visit 1
A     BMI        3    Visit 1

data have;
input id    var $   val visit $;
cards;
A height 5 base
A weight 3 base
A bmi 1 base
A height 2 visit1
A weight 4 visit1
A bmi 3 visit1
;

我想创建一个3列,保留所有访问的基本值:

 ID   Variable  Value   Visit    Height   Weight   BMI 
 A     Height     5     Base       5        3      1    
 A     Weight     3     Base       5        3      1 
 A     BMI        1     Base       5        3      1 
 A     Height     2    Visit 1     5        3      1  
 A     Weight     4    Visit 1     5        3      1    
 A     BMI        3    Visit 1     5        3      1

我会尝试通过保留“基本”值来尝试此操作,但是我对如何使用它有些困惑。

2 个答案:

答案 0 :(得分:1)

proc sort data=mydata out=base;
  where visit='base';
  by id;
run;

data base;
  set base;
  by id;

  if first.id then call missing(weight, height, bmi);
  retain height weight bmi;
  if var = 'Height' then height = val;
  else if var = 'Weight' then weight = val;
  else bmi = val;

  if last.id then output;
  keep height weight bmi id;

run;

proc sql;
  create table withbase as
  select * from mydata a full join base b on a.id = b.id;

答案 1 :(得分:1)

按照您的要求将BASELINE添加到每个obs似乎不是很有用。这样的事情怎么样。

data have;
input id:$1. var $ val visit $;
cards;
A height 5 base
A weight 3 base
A bmi 1 base
A height 2 visit1
A weight 4 visit1
A bmi 3 visit1
;;;;
   run;
proc sort data=have;
   by id var visit;
   run;
proc print;
   run;
data maybe;
   do until(last.var);
      set have;
      by id var;
      if visit eq 'base' then baseline=val;
      if not missing(baseline) then do;
         change = val - baseline;
         end;
      output;
      end;
   run;
proc print;
   run;

enter image description here