Hi:
There's a big difference between ending the program and checking the rest of the rows.
But there's still a way to get only the first obs that meets your criteria by setting flags -- don't need a DO UNTIL loop. In this example, the GETFIRST data set only contains 1 obs -- for the first girl whose age was GT 14. The CKFLAGS data set is all the observations and the flags, so you can see the behavior of the FRSTGIRL flag for this one condition across all obs.
[pre]
**4) get the first of the criteria;
data getfirst ckflags;
set sashelp.class;
** frstgirl is the flag for whether ;
** the first girl was found;
** I want to retain this flag;
retain frstgirl 0;
** Only proceed to inside test if
frstgirl is still 0;
if frstgirl = 0 then do;
if sex = 'F' and age gt 14 then do;
frstgirl=1;
output getfirst;
end;
end;
output ckflags;
run;
proc print data=getfirst;
title '4) Get First Obs that meets criteria';
title2 'should only be 1 obs in this file';
run;
proc print data=ckflags;
title '4) See the status of all the flags using this logic';
title2 'The output performed on the obs when frstgirl flag changed from 0 to 1';
run;
[/pre]
I chose to output to a data set rather than end the program (stop reading the file) because if you have other criteria or other tests, then you may want to include flags for those tests. If you needed to test multiple criteria (such as getting the first female student where AGE GT 14 and the first male student where AGE GT 14) then you'd need more flags:
[pre]
**5) get the first of the criteria;
data multcriteria allflags;
set sashelp.class;
retain frstgirl frstguy 0;
if frstgirl = 0 then do;
if sex = 'F' and age gt 14 then do;
frstgirl=1;
output multcriteria;
end;
end;
if frstguy = 0 then do;
if sex = 'M' and age gt 14 then do;
frstguy=1;
output multcriteria;
end;
end;
output allflags;
run;
[/pre]
There are, no doubt, other solutions to the problem, but this to me, is the most straightforward without using DO loops, because it takes advantage of the fact that each obs is read only 1 time and tested against the criteria and then flags are set that control whether subsequent obs need to be tested at all -- for this criteria. Output is controlled for every obs based on the status of the flags and only happens inside the IF statement for each test. If an obs could satisfy more than one test, then this logic might not be appropriate and you might need to use ELSE and/or decide where to control the output differently.
The CKFLAGS and ALLFLAGS data sets are really included in the code for instructional purposes so that you can print out the whole dataset to see how the flags were set for every obs.
cynthia
... View more