09-28-2016 06:10 AM
I have dataset with multiple observations per subject.
I want to create a dataset to keep only one record per subject where the variable 't' closest to value 24.
Any help to do this in sas?
09-28-2016 06:33 AM
Simplest way I can think of is to create an offset var, then sort by offset and take n=1:
data want; set have; offset=ifn(t<24,24-t,t-24); run; proc sort data=want; by offset; run; data want; set want (obs=1); run;
Not tested as not typing in test data - post as a datastep next time.
09-28-2016 06:57 AM
or this way:
data have; input ID t v; datalines; 1 30 2.95 1 22 5.11 1 23 2.33 1 21 4.3 1 28 2.28 ; run; proc sql; create table want as select id, t, v from have group by id having min(abs(24-t))=abs(24-t) ; quit;
09-28-2016 07:22 AM
Since you indicate there are multiple IDs:
data int; set have; offset = abs(t-24); run; proc sort data=int; by id offset; run; data want; set int; by id; if first.id; drop offset; run;
For larger datasets, this might be more performant than doing it with SQL.