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
Carol|F|14|62.8|102.5
Henry|M|14|63.5|102.5
James|M|12|57.3|83
;;;;
run;
data males;
set class;
where sex="M";
run;
data preteen;
set class;
where age in (11,12);
run;
- To filter observations in SAS, we write a
DATAstep with aSETand aWHEREclause. where sex="M";keeps only rows where the sex variable equals the literal string M. SAS applies the WHERE at read time, which is efficient — rows that fail the test never enter the new dataset at all.- For matching against a list of values, we use the
inoperator:where age in (11, 12);keeps rows whose age is 11 or 12. - The value list in
incan contain literals or macro variables. Character values need quotes; numerics do not. - The output datasets —
malesandpreteen— have the same variable structure as CLASS but fewer rows.