[英]Transpose data into rows, multiple columns
My data looks like below: 我的数据如下所示:
ID Month Variable
1 1 Y
1 2 N
1 3 Y
2 1 Y
2 2 Y
2 3 Y
I need to transpose it such that the ID and month are in rows and Variable values in columns Outputdata: 我需要对它进行转置,以使ID和month在行中,而Variable值在Outputdata列中:
ID Month Variable_Y Variable_N
1 1 Y
1 2 N
1 3 Y
2 1 Y
2 2 Y
2 3 Y
How do I do this? 我该怎么做呢?
Using the PROC TRANSPOSE it's pretty straightforward. 使用PROC TRANSPOSE非常简单。
proc transpose data=from out=transposed (drop=_name_)
prefix=variable_
;
by id month;
id variable;
var variable;
run;
Or using a datastep: 或使用数据步骤:
data test_output (drop = variable);
set test_input;
format Variable_Y Variable_N $1.;
if variable = "Y" then do;
Variable_Y = "Y";
call missing (Variable_N);
end;
else if variable = "N" then do;
Variable_N = "N";
call missing (Variable_Y);
end;
run;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.