主题访问的基线平均值 (SAS)

Baseline mean by subject visits (SAS)

我正在尝试计算特定访问的基线平均值。例如,如果受试者没有指定就诊,则将忽略受试者重新计算基线平均值。

这是数据:

Subject            Visit              Value
001                Baseline           10
001                Visit 2            11
001                Visit 3            12
001                Visit 4            13
002                Baseline           11
002                Visit 2            12
002                Visit 4            13
002                Visit 5            14
003                Baseline           12
003                Visit 3            13
003                Visit 4            14
003                Visit 5            15

我想获得以下信息:

Visit             BaselineMean       VisitMean
Baseline          11                 11
Visit 2           10.5               11.5
Visit 3           11                 12.5
Visit 4           11                 13.3
Visit 5           11.5               14.5

这是我每次访问的平均值 table:

proc sql;
create table want as
select 
    visit, 
    mean(value) as meanValue
from have
group by visit;

如有任何见解,我们将不胜感激。

考虑两个聚合的连接,其中一个聚合自身使用自连接:

proc sql;
   CREATE TABLE want as 
   SELECT bagg.Visit, bagg.BaselineMean, vagg.VisitMean
   FROM 
     (SELECT t2.Visit, MEAN(t1.Value) AS BaselineMean
      FROM have t1
      INNER JOIN have t2
         ON t1.Subject = t2.Subject 
        AND t1.Visit = 'Baseline'
      GROUP BY t2.Visit) bagg

   INNER JOIN
     (SELECT Visit, MEAN(Value) AS VisitMean
      FROM have 
      GROUP BY Visit) vagg
   ON bagg.Visit = vagg.Visit;
quit;

SQL Demo

首先创建一个 table 使每个受试者的基线值如下:

proc sql;
create table baseline as 
    select distinct subject, value
        from t1
    where visit = 'Baseline'
;

然后用基线值扩充主 table。请注意,在没有主题基线记录的情况下使用合并:

proc sql;
    create table inter as 
        select t1.*m, coalesce(b.value, 0) as b_val 
            from t1 left join baseline b  
                on t1.subject = b.subject
;
quit;

最终计算基线和访问的平均值如下:

  proc sql;
        select visit, mean(b_val) as BaselineMean, mean(value) as visitMean
            from inter 
    group by visit
;
quit;

您可以计算一个子 select 的聚合统计数据,该子 select 本身使用一个相关的子查询,该子查询 select 是受试者的基线值。

proc sql;
  create table want as
  select 
    visit
  , mean(baseline) as baselinemean format=5.1
  , mean(value) as visitmean       format=5.1
  from 
    ( select 
        visit
      , value
      , ( select value 
          from have as inner 
          where inner.subject = outer.subject 
            and inner.visit='Baseline'
        ) as baseline
      from have as outer
    )
  group by visit
  ;
quit;