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
;;;;
run;
data males;
set class;
where sex="M";
drop sex;
run;- To filter rows and drop columns in a single pass, we combine
WHEREanddropinside one DATA step. set class;reads the input.where sex="M";restricts the incoming rows.drop sex;removes the sex column from the output.- Order matters conceptually: the WHERE filter runs as rows are read, and the DROP removes variables from the output buffer. So the sex value is available for the WHERE test even though it does not appear in the final dataset.
- We could equally write
keep name age height weight;— the result is identical.dropis handier when there are only a few columns to remove;keepis clearer when there are only a few to keep. - Output
maleshas the male rows from CLASS and every column except sex.