如何在一列中转置具有多个项响应的数据?在SAS
我有一天多个会话中报告的数据,跨越多天。但是,数据设置为以下格式:如何在一列中转置具有多个项响应的数据?在SAS,sas,data-management,Sas,Data Management,我有一天多个会话中报告的数据,跨越多天。但是,数据设置为以下格式: Participant Day Session Item_id Item_info. Response 1 1 1 1 "In the past.." 2 1 1 1 2 "In the present
Participant Day Session Item_id Item_info. Response
1 1 1 1 "In the past.." 2
1 1 1 2 "In the present.." 5
1 1 1 3 "In the future.." 10
1 1 2 1 "In the past.." 4
1 1 2 2 "In the present.." 5
1 1 2 3 "In the future.." 3
1 3 3 1 "In the past.." 6
1 3 3 2 "In the present.." 6
1 3 3 3 "In the future.." 8
2 1 1 1 "In the past.." 7
2 1 1 2 "In the present.." 7
2 1 1 3 "In the future.." 7
等等
我的目标是使数据如下所示:
Participant Day Session In the Past... In the Present... In the future...
1 1 1 2 5 10
1 1 2 4 5 3
1 3 3 6 6 8
2 1 1 7 7 7
我试过使用PROC转置
proc sort data=use out= x ;
by participant day session prompt_id;
run;
proc transpose data=x out=x2;
by participant day session prompt_id;
var response;
run;
但那没用。相反,它看起来像是基于会话创建了新列
我不确定我是否只是没有正确理解proc转置,或者是否还有其他事情或技术需要做
提前感谢您的帮助 您几乎已经解决了这个问题,但是需要在proc transpose中使用id和idlabel语句来获得您想要的结果 这里有一个例子
data use;
participant=1; day=1; session=1; item_id=1; item_info="In the past.." ; Response=2 ; output;
participant=1; day=1; session=1; item_id=2; item_info="In the present.."; Response=5 ; output;
participant=1; day=1; session=1; item_id=3; item_info="In the future.." ; Response=10; output;
participant=1; day=1; session=2; item_id=1; item_info="In the past.." ; Response=4 ; output;
participant=1; day=1; session=2; item_id=2; item_info="In the present.."; Response=5 ; output;
participant=1; day=1; session=2; item_id=3; item_info="In the future.." ; Response=3 ; output;
participant=1; day=1; session=3; item_id=1; item_info="In the past.." ; Response=6 ; output;
participant=1; day=1; session=3; item_id=2; item_info="In the present.."; Response=6 ; output;
participant=1; day=1; session=3; item_id=3; item_info="In the future.." ; Response=8 ; output;
participant=2; day=1; session=1; item_id=1; item_info="In the past.." ; Response=7 ; output;
participant=2; day=1; session=1; item_id=2; item_info="In the present.."; Response=7 ; output;
participant=2; day=1; session=2; item_id=3; item_info="In the future.." ; Response=7 ; output;
run;
options validvarname=v7; *<- to avoid numbers as varnames;
proc sort data=use;
by participant day session;
run;
proc transpose data=use out=t_use(drop=_name_);
id item_id; /* if you prefer the text as var name use item_info instead */
idlabel item_info;
by participant day session;
var response;
run;
proc print data=t_use label;
run;
数据使用;
参与者=1;日=1;会话=1;项目id=1;item_info=“过去…”;响应=2;产出;
参与者=1;日=1;会话=1;项目id=2;item_info=“在当前..”;响应=5;产出;
参与者=1;日=1;会话=1;项目id=3;item_info=“将来…”;响应=10;产出;
参与者=1;日=1;会话=2;项目id=1;item_info=“过去…”;反应=4;产出;
参与者=1;日=1;会话=2;项目id=2;item_info=“在当前..”;响应=5;产出;
参与者=1;日=1;会话=2;项目id=3;item_info=“将来…”;反应=3;产出;
参与者=1;日=1;会话=3;项目id=1;item_info=“过去…”;反应=6;产出;
参与者=1;日=1;会话=3;项目id=2;item_info=“在当前..”;反应=6;产出;
参与者=1;日=1;会话=3;项目id=3;item_info=“将来…”;反应=8;产出;
参与者=2人;日=1;会话=1;项目id=1;item_info=“过去…”;反应=7;产出;
参与者=2人;日=1;会话=1;项目id=2;item_info=“在当前..”;反应=7;产出;
参与者=2人;日=1;会话=2;项目id=3;item_info=“将来…”;反应=7;产出;
跑
选项validvarname=v7*
您的输出应该如下所示项目信息值是否包含实际的双引号?嗨,Richard,不,它们实际上不包含任何引号。这只是我表示它们是字符而不是值的方式。嗨,Niqua,这是我的输出与您描述/显示的内容类似的内容。然而,我的问题是,响应2、5和10在同一个会话中,因此我需要它们在同一行中。这可以通过创建新列来实现,sum(t1'过去',t1'现在',t1'将来')
因为其中两个总是会丢失,所以应该将值叠加到单个列中
PROC TRANSPOSE DATA=WORK.HAVE
OUT=WORK.WANT
NAME=Source
LABEL=Label
;
BY Participant Day Session;
ID "Item_Info."n;
VAR Response;
RUN;