我有一个数据集,在不同的时间点完成了大量的实验室测试。我试图将数据集从长到大转换,但问题是实验室测试发生在不同的时间点,具体取决于测试类型。当我转置它时,我失去了分辨结果来自哪个时间点的能力。请参阅下面的示例代码:
*Create test data;
data long;
do subject=1 to 10;
do test=1 to 3;
do visit=1 to 3;
result=rand("Uniform");
output;
end; end; end;
run;
*Now remove records at certain visits depending upon the test type;
data long; set long;
if test=2 and visit=2 then delete;
if test=3 and visit=1 then delete;
run;
*Sort and transpose;
*Test 2 should only be at visit 1 and 3, and test 3 at visits 2 and 3;
*This transpose does not accomplish that goal;
proc sort data=long; by subject test visit;run;
proc transpose data=long out=wide;
by subject test ;
var result;
run;
答案 0 :(得分:1)
对于您的示例数据,您只需要在PROC TRANSPOSE代码中添加ID语句。这样它将使用VISIT的值来命名结果列。您可能还想将PREFIX =选项添加到PROC TRANSPOSE语句中。
proc transpose data=long out=wide prefix=visit;
by subject test ;
id visit ;
var result;
run;