我想在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] + '; '
}
然后可以通过“注释”列逐行压缩数据。任何有关如何通过SAS中的DATA或PROC SQL步骤完成此操作的见解将不胜感激。
答案 0 :(得分:2)
使用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;
答案 1 :(得分:2)
出于报告目的,当Proc PRINT
和BY
语句列出相同的变量名称时,ID
具有特殊的输出布局。当该组有多行时,这些组将分隔,并且该组的值将不重复。
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;