0
votes

I'm trying to save observations inside a one year interval per id in SAS.

I have different amounts of obsevations per id. Each observation has a visit date called date1 and a reference date called datum_sistahandelse(wich do not change over observations per id, but changes between ids). The dates are in format YYYY-MM-DD. I want to keep only the observations where date1 is later than datum_sistahandelse but no more than a year later. So if for ID #1 datum_sistahandelse=2014-02-03 then i want to keep all observations where date1 is between 2014-02-04 and 2015-02-04. And for ID #2 datum_sistahandelse=2015-05-13 i want to keep all observations where date1 is between 2015-05-14 and 2016-05-14. And so on

Thanks

2

2 Answers

0
votes

Ok, so i figured it out:

data efterdc5;
set efterdc4;
ettarefter=datum_sistahandelse;
format ettarefter yymmdd10.;
run;

data efterdc5;
set efterdc5;
ettarefter2=intnx('year',ettarefter,1,'same');
format ettarefter2 yymmdd10.;
run;

data efterdc5;
set efterdc5;
where datum_sistahandelse<date1<=ettarefter2;
drop ettarefter;
run;

This gave me the result of keeping the observations one year later than datum_sistahandelse

0
votes

Because the reference date value is in the column datum_sistahandelse which contains a static value over the group, you can perform the sub-setting with a single step

data want;
  set have;
  where date1 between sistahandelse+1 and intnx('year',sistahandelse,1,'same');
run;