Stefano Lombardi
Stefano Lombardi

Reputation: 1591

Selecting groups of records using FIRST.var

My dataset is:

ID     AGE

1      65
1      66
1      67
1      68
1      69
1      70
1      71
2      70
2      71
2      72
3      68
3      69
3      70

 [...]

My (basic) question is: which is the most direct way for obtaining a dataset with ID records starting with 65 <= AGE <= 68? (in the above example I would like to get the first 7 rows and the last three). Thanks!

Upvotes: 0

Views: 1119

Answers (2)

Joe
Joe

Reputation: 63424

Just to have another method...

proc sql;
  delete from input_dataset I where not exists 
    (select 1 from input_dataset D where I.id=D.id having 65 le min(age) le 68);
quit;

If you want to create a new dataset, the same basic query would work as part of a SELECT, reversing the NOT.

Upvotes: 2

vasja
vasja

Reputation: 4792

data input_dataset;
input ID     AGE;
cards;
1      65
1      66
1      67
1      68
1      69
1      70
1      71
2      70
2      71
2      72
;
run;

proc sort data=input_dataset out=sorted;
by ID;
run;

data work.first_age65to68;
  set sorted;
  retain keepit 0;
  by ID;
  if first.ID then do;
      if AGE ge 65 and AGE le 68 then keepit=1;
      else keepit=0;
  end;
  if keepit;
  drop keepit;
run;

Upvotes: 1

Related Questions