OU. I just realize what you are looking for . Code: Program.sas data have; informat id $4. surgery_date medication_date mmddyy10. medication_name $4.; format surgery_date medication_date mmddyy10. ; input id surgery_date medication_date medication_name; datalines; 1 03/15/2010 05/01/2010 A 1 03/15/2010 05/01/2010 B 1 03/15/2010 09/01/2010 C 1 03/15/2010 11/01/2010 D 1 05/01/2011 09/01/2011 E 1 05/01/2011 09/01/2011 F 1 05/01/2011 09/01/2011 G 1 05/01/2011 12/01/2011 H ; run; proc sql; select max(count) into : n separated by ' ' from (select count(*) as count from have group by id,surgery_date,medication_date); quit; proc summary data=have nway; class id surgery_date medication_date; output out=temp(drop=_:) idgroup(out[&n] (medication_name )=); run; proc sql; select max(count) into : m separated by ' ' from (select count(*) as count from temp group by id,surgery_date); quit; proc summary data=temp nway; class id surgery_date; output out=want(drop=_:) idgroup(out[&m] (medication_date medication_name_1-medication_name_&n )=); run; proc sql; select name into : list separated by ' ' from dictionary.columns where libname='WORK' and memname='WANT' and lowcase(name) like 'medication%' order by input(scan(name,-1,'_'),best8.),name; quit; data final_want; retain id surgery_date &list ; set want; run; Xia Keshan
... View more