The PROC SUMMARY offers a neat compact single-pass solution. It will certainly use a lot less disk input/output resources than the PROC SORT solution, and will probably be a lot faster - assuming there is no memory constraint.
BUT ... does your data have the possibility of tied maximum time_flag values for a given ID/VAR1/VAR2?
If not, then ignore the rest of this comment.
But if it does, then the PROC SUMMARY might not likely give the same result as the PROC SORT ... if LAST.VAR2 solution. It will choose different records (with possibly different VAR3/VAR4 values) among the tied records.
This is because the default behavior of PROC SORT is to preserve the original order (from the unsorted dataset) of tied records. So that solution would always choose the latest of the tied records.
A quick test of PROC SUMMARY with ties suggests it would always choose the first of such ties. At least it did so in the test below:
data ties;
set sashelp.class (keep=name sex age weight);
order='First'; output;
order='Last' ; output;
run;
proc summary nway data=ties;
class sex age;
output out=summ_want (drop=_:) idgroup (max(weight) out (name weight order)=);
run;
proc sort data=ties;
by sex age weight;
run;
data sort_want;
set ties;
by sex age;
if last.age;
run;
Dataset summ_want has order='First' in every output record, but the PROC SORT approach always has ORDER='Last'.
... View more