【问题标题】:Creating a summary column in SAS from multiple columns在 SAS 中从多个列创建汇总列
【发布时间】:2019-05-11 09:55:12
【问题描述】:

我想在 SAS 中创建一个单独的列,用于汇总数据集中每个人的几列。数据如下所示:

Subject      VisitNumber   Exam      Result       Comments
001          1             Blood     Negative     Will return for more testing
001          1             BP        100          Score is in normal range
001          1             Vision    20/20        No issues with eyesight 
002          5             BMI       19           Within healthy range
002          5             Hearing   Good         Patient hears well
002          5             Drug      Negative     Subject passed drug test

每个受试者的信息及其后续访问次数应总结如下:

Subject    VisitNumber      Summary
001        1                Exam: Blood, Result: Negative, Comments: Will return for more testing; Exam: BP, Result: 100, Comments: Score is normal range; Exam: Vision, Result: 20/20, Comments: No issues with eyesight
002        5                Exam: BMI, Result: 19, Comments: Within healthy range; Exam: Hearing, Result: Good, Comments: Patient hears well; Exam: Drug, Result: Negative, Comments: Subject passed drug test

可以通过以下方式在 R 中执行此操作:

for (i in 1:length(data$Subject))

{
data$Summary[i] = data$Comments[i] = 'Exam: ' + Exam[i] + ', Result: ' + Result[i] + ', Comments: ' + Comments[i] + '; '
}

然后可以通过 Comments 列逐行压缩数据。任何关于如何通过 SAS 中的 DATA 或 PROC SQL 步骤完成此操作的见解将不胜感激。

【问题讨论】:

    标签: arrays sas


    【解决方案1】:

    使用 SAS 连接函数。

    data want;
       set have;
       by subject notsorted;
       length summary $500.;
       retain summary;
       summary=catx(';',summary, catx(',', cats('Exam:',Exam),cats('Result:',Result),cats('Comments:',Comments)));
       if last.subject then output;
       keep Subject VisitNumber summary;
    run;
    

    【讨论】:

    • 我认为您缺少在每个 by 组的开头将 Summary 设置为 null 的语句。类似于:if first.subject then summary='' ; 这样的值不会跨主题保留。
    • @Quentin 添加 first.subject 然后 summary = '' 解决了跨组保留值的问题。但是,如果主题只有一行,则删除该值。有什么方法可以保留 first.subject 并且不保留它?
    • @statsguyz 在保留语句之后立即添加它。在分配摘要的赋值语句之前。
    【解决方案2】:

    BYID 语句列出相同的变量名称时,出于报告目的Proc PRINT 具有特殊的输出布局。这些组将被分隔,并且当组有超过一行时,组的值将不重复

    data have;
    input
    Subject&$    VisitNumber&  Exam&$    Result&$     Comments&$200.; datalines;
    001          1             Blood     Negative     Will return for more testing
    001          1             BP        100          Score is in normal range
    001          1             Vision    20/20        No issues with eyesight 
    002          5             BMI       19           Within healthy range
    002          5             Hearing   Good         Patient hears well
    002          5             Drug      Negative     Subject passed drug test
    run;
    
    ods html style=Journal;
    title "Subject visit examinations";
    proc print data=have;
      by subject visitnumber;
      id subject visitnumber;
    run;
    

    【讨论】:

      猜你喜欢
      • 2018-08-03
      • 1970-01-01
      • 2020-11-27
      • 2021-04-28
      • 1970-01-01
      • 2013-04-20
      • 2017-07-28
      • 2021-10-27
      • 2013-03-29
      相关资源
      最近更新 更多