【问题标题】:SAS: recode variable across group of observations, including overriding optionSAS:跨组观察重新编码变量,包括覆盖选项
【发布时间】:2014-05-01 01:20:10
【问题描述】:

我有一个包含患者 ID 和种族的大型 SAS 数据集。这是一个纵向数据集,其中每次观察都代表一次对医院的访问。有许多观察结果缺少种族信息,但针对同一患者 ID 的其他访问已指示种族。我使用下面的代码来解决对缺少种族的给定患者 ID 的任何观察结果,只要另一次访问有该信息:

data need;
   do until (last.id);
      set have;
      by id;
      if not missing(race) then newrace=race;
      if missing(race) then race=newrace;
      output;
   end;
run;

我的问题是 - 当患者在选项中显示多个种族时,我如何记录?我如何确定一个比其他人更占主导地位/压倒一切(即对于患者 342,有 3 个种族 = 2 的 obs 和 2 个种族 = 4 的 obs;我们想要任何种族 = 4 的指示来确定 newrace = 4对于所有 342 号患者的 obs)。

谢谢!

【问题讨论】:

  • 我有点困惑,在您的示例中选择 race=4 的逻辑是什么?你总是选择价值最高的那个吗?一旦我知道了这一点,就会有一个直接的解决方案。
  • 在患者数据库中,对于西班牙裔,race=(1,2,3,4,5) 和 race=4。在我们正在研究的人群中,任何西班牙裔的迹象(即使对同一患者的其他观察表明另一个种族)都超过了其他种族的迹象。发生这种情况的另一个例子是,如果患者是多种族的,并且可能在第一次就诊时表示种族=2,在第二次就诊时表示种族=4,在第三次就诊时表示种族=5。总的来说,我希望能够将此患者的所有种族重新编码为 =4。

标签: sas


【解决方案1】:

我这样做的方法是为患者 ID 创建一个格式。这不仅解决了您当前的问题,而且可能在其他步骤中很有用,因为它可以在 procs 中使用。

data for_fmt;
set have;
by id;
retain label;
retain fmtname 'IDRACEF';
start=id;
if race=4 then label=4; *or you could have label='Hispanic', also - could use this to convert to character strings;
else label=coalesce(label,race); *otherwise only change race if label is missing;
if last.id then output;
keep start label fmtname hlo;
if _n_=1 then do;
 start=.;
 label=.; *or 'MISSING' or something else indicating a nonmatch;
 hlo='o';
 output;
end;
run;

proc format cntlin=for_fmt;
quit;

然后您可以使用IDRACEF. 作为格式,在列上使用format(例如在proc means 中),或者使用put 语句。

【讨论】:

    【解决方案2】:

    此答案假定 4 是最重要的种族,并且如果一个 id 有多个种族但其中之一是 4,则所有种族值都将替换为 4。此外,如果给定 id 有多个种族,则无其中是 4 个,这段代码本质上是随机选择哪一个来替换缺失值。

    data races (drop=race);
    do until (last.id);
    set have;
    by id;
    if newrace ne 4 then newrace = race;
    end;
    output;
    run;
    
    data need (drop=newrace);
    merge have races;
    by id;
    if missing(race) then race=newrace;
    if newrace = 4 then race = 4;
    run;
    

    第一部分创建一个数据集“races”,其中race 应替换每个id 的缺失值。第二个将其合并到原始集合中,并用“races”中的比赛替换缺失。

    【讨论】:

    • 您可能应该在比赛中添加keep 语句以仅保留 newrace 和 ID。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    相关资源
    最近更新 更多