SAS (Base SAS)+
data CLASS;
infile datalines dlm='|' dsd missover;
input Name : $8. Sex : $1. Age : best32. Height : best32. Weight : best32.;
label ;
format ;
datalines4;
Alfred|M|14|69|112.5
Alice|F|13|56.5|84
Barbara|F|13|65.3|98
Carol|F|14|62.8|102.5
Henry|M|14|63.5|102.5
James|M|12|57.3|83
Jane|F|12|59.8|84.5
Janet|F|15|62.5|112.5
Jeffrey|M|13|62.5|84
John|M|12|59|99.5
Joyce|F|11|51.3|50.5
Judy|F|14|64.3|90
Louise|F|12|56.3|77
Mary|F|15|66.5|112
Philip|M|16|72|150
Robert|M|12|64.8|128
Ronald|M|15|67|133
Thomas|M|11|57.5|85
William|M|15|66.5|112
;;;;
run;
proc sort data=class;
by age;
run;
data only_one_in_group;
set class;
by age;
if first.age and last.age;
run;- When both
first.varandlast.varare TRUE for the same row, that row is the only one in its group. - After
proc sort data=class; by age;, the DATA step reads CLASS withby age;so thefirst.age/last.ageflags are available. if first.age and last.age;is a subsetting IF — SAS writes out the row only when the condition is TRUE, which happens only when the Age group has exactly one row.- No
outputorretainstatement is needed; the subsetting IF is all we need to thin the output to singleton groups. - After running, ONLY_ONE_IN_GROUP contains those CLASS rows whose Age appears only once in the sample.
- A good sanity check: a frequency count of Age on CLASS should show frequency 1 for exactly the Age values we see in ONLY_ONE_IN_GROUP.