Bosley
Bosley

Reputation: 231

SAS transpose when data are missing

I have a data set with a large number of lab tests done at different points in time. I am trying to transpose the data set from long to wide, but the problem is that the lab tests occur at different time points depending upon the type of test. When I transpose it, I'm losing my ability to tell which time point the result is from. See my example code below:

*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;

Upvotes: 0

Views: 2598

Answers (1)

Tom
Tom

Reputation: 51611

For your example data you just need to add an ID statement to your PROC TRANSPOSE code. That way it will use value of VISIT to name the resulting columns. You might also want to add the PREFIX= option to the PROC TRANSPOSE statement.

proc transpose data=long out=wide prefix=visit;
  by subject test ;
  id visit ;
  var result;
run;

Upvotes: 1

Related Questions