SAS (Base SAS)+
data AE;
infile datalines dlm='|' dsd missover;
input subjid : $10. term : $20. stdtc : $10.;
label ;
format ;
datalines4;
1001|Headache|2010-01-05
1001|Headache|2010-01-08
1001|Nausea|2010-01-08
1002|Headache|2010-02-15
;;;;
run;
data AE1;
infile datalines dlm='|' dsd missover;
input subjid : $10. term : $20. stdtc : $10.;
label ;
format ;
datalines4;
1001|Headache|2010-01-05
1001|Headache|2010-01-08
1001|Nausea|2010-01-08
1002|Headache|2010-02-15
1002|Headache|2010-02-15
;;;;
run;
*------------------------------------------------------------------------------;
*nodupkey;
*------------------------------------------------------------------------------;
proc sort data=ae;
by subjid term stdtc;
run;
proc sort data=ae out=nodupkey nodupkey;
by subjid term;
run;
*------------------------------------------------------------------------------;
*noduprec;
*------------------------------------------------------------------------------;
proc sort data=ae1 out=noduprec noduprec;
by _all_;
run;
infile datalines dlm='|' dsd missover;
input subjid : $10. term : $20. stdtc : $10.;
label ;
format ;
datalines4;
1001|Headache|2010-01-05
1001|Headache|2010-01-08
1001|Nausea|2010-01-08
1002|Headache|2010-02-15
;;;;
run;
data AE1;
infile datalines dlm='|' dsd missover;
input subjid : $10. term : $20. stdtc : $10.;
label ;
format ;
datalines4;
1001|Headache|2010-01-05
1001|Headache|2010-01-08
1001|Nausea|2010-01-08
1002|Headache|2010-02-15
1002|Headache|2010-02-15
;;;;
run;
*------------------------------------------------------------------------------;
*nodupkey;
*------------------------------------------------------------------------------;
proc sort data=ae;
by subjid term stdtc;
run;
proc sort data=ae out=nodupkey nodupkey;
by subjid term;
run;
*------------------------------------------------------------------------------;
*noduprec;
*------------------------------------------------------------------------------;
proc sort data=ae1 out=noduprec noduprec;
by _all_;
run;
- SAS folds deduplication into PROC SORT through two options:
nodupkeyandnoduprec. proc sort data=ae out=nodupkey nodupkey; by subjid term;keeps the first row per (subjid,term) combination and drops any repeats within that key.- The option looks only at the variables listed in the
bystatement — columns outside the key, such asstdtc, are ignored when deciding whether a row is a duplicate. - Because
nodupkeykeeps the first occurrence, a preceding sort is important: it decides which record survives when several match on the key. proc sort data=ae1 out=noduprec noduprec; by _all_;drops rows that match an earlier row on every single column.by _all_;is the SAS shorthand for "use every variable in the dataset", which is the right key list for full-row deduplication.- After running, NODUPKEY has one row per subject-term pair; NODUPREC has the AE1 rows with exact-copy repeats removed.