If I've interpreted what you want correctly, this will come close. I'm assuming that you want multiple body category examinations and diagnoses per patient. I'm also assuming that trialctrpt is the patient id, and patient is some other way of identifying them. Don't worry too much about the first three steps - it's just my attempt at mocking up some test data.
Rather than trying to do it all in one transpose, which then requires lots of complicated arrays and post-processing, I went over the patient/body/exam/diag data three times, and then rejoined back with the original dataset.
data medhistory(drop=body exam diag)
patient(keep=trialctrpt body exam diag);
length TrialID Center Patient Visit ExamDt 8;
length Body exam diag $ 20;
length Sex Race Consent ConsentDt Wt RandomDt PerformStat Trt StopReas TrtGive Age TrtDt CtrPt TrialCtrPt 8;
array body_a[17] $ 20 _temporary_ ('head', 'eye', 'ear', 'nose', 'throat', 'neck', 'lymph node', 'breast', 'heart', 'abdomen',
'lung/thorax', 'genitourinary', 'extremities', 'musculoskeletal', 'skin', 'neurologic',
'additional');
call missing(of _all_);
do trialctrpt = 1 to 100;
do i = 1 to int(ranuni(225465114) * 3) + 1;
body = body_a[int(ranuni(225465114) * dim(body_a)) + 1];
exam = ifc(ranuni(225465114) > .1, 'normal', 'abnormal');
diag = ifc(exam = 'abnormal', 'indicated', ' ');
output patient; /* Multiple observations per patient (in this example, up to 3) */
end;
output medhistory; /* Single observations per patient */
end;
stop;
keep TrialID Center Patient Visit ExamDt Body exam diag
sex Race Consent ConsentDt Wt RandomDt PerformStat Trt StopReas TrtGive Age TrtDt CtrPt TrialCtrPt ;
run;
/*
Clean data to stop multiple body entries per patient
*/
proc sort data=patient noequals;
by trialctrpt body exam;
run;
data patient;
set patient;
by trialctrpt body;
if first.body;
run;
/*
Get the three different transpositions
*/
proc transpose data=patient out=transbody(drop=_name_) prefix=body_;
by trialctrpt;
var body;
run;
proc transpose data=patient out=transexam(drop=_name_) prefix=exam_;
by trialctrpt;
var exam;
run;
proc transpose data=patient out=transdiag(drop=_name_) prefix=diag_;
by trialctrpt;
var diag;
run;
/*
Join back with the patient records.
*/
data medhistory;
set medhistory;
set transbody;
set transexam;
set transdiag;
run;
... View more