I have a dataset with variables ID VISITNUM VISIT DATE,
I should write query in such a way that, for every id the difference between two successive visits should not be more than 6 weeks. If it is more than 6 weeks I should output those discrepancies. I am attaching sample data.
100010001 1 CYCLE 1 2014-09-15
100010001 2 CYCLE 2 2014-10-27
100010001 3 CYCLE 3 2014-11-27
100010002 1 CYCLE 1 2014-10-13
100010002 2 CYCLE 2 2014-11-24
100010003 1 CYCLE 1 2015-02-08
100010003 1 CYCLE 1 2015-04-08
100010004 1 CYCLE 1 2015-04-28
100020001 1 CYCLE 1 2014-12-04
100020001 2 CYCLE 2 2015-01-15
100020001 3 CYCLE 3 2015-03-02
100020001 4 CYCLE 4 2015-04-13
100020002 1 CYCLE 1 2015-03-18
100020002 2 CYCLE 2 2015-04-29
100030002 1 CYCLE 1 2014-11-03
100030002 2 CYCLE 2 2014-12-22
100030002 3 CYCLE 3 2015-02-09
100030002 4 CYCLE 4 2015-03-23
100030003 1 CYCLE 1 2015-05-04
140010004 1 CYCLE 1 2015-02-11
140010004 2 CYCLE 2 2015-03-27
data have;
input id $10. visit cycle $ date yymmdd10.;
format date yymmdd10. ;
cards;
100010001 1 CYCLE-1 2014-09-15
100010001 2 CYCLE-2 2014-10-27
100010001 3 CYCLE-3 2014-11-27
100010002 1 CYCLE-1 2014-10-13
100010002 2 CYCLE-2 2014-11-24
100010003 1 CYCLE-1 2015-02-08
100010003 1 CYCLE-1 2015-04-08
100010004 1 CYCLE-1 2015-04-28
100020001 1 CYCLE-1 2014-12-04
100020001 2 CYCLE-2 2015-01-15
100020001 3 CYCLE-3 2015-03-02
100020001 4 CYCLE-4 2015-04-13
100020002 1 CYCLE-1 2015-03-18
100020002 2 CYCLE-2 2015-04-29
100030002 1 CYCLE-1 2014-11-03
100030002 2 CYCLE-2 2014-12-22
100030002 3 CYCLE-3 2015-02-09
100030002 4 CYCLE-4 2015-03-23
100030003 1 CYCLE-1 2015-05-04
140010004 1 CYCLE-1 2015-02-11
140010004 2 CYCLE-2 2015-03-27
;
run;
proc sql;
select b.*,ifn(intck('week',a.date,b.date)>6,1,0) as flag from have a right join have b
on a.id=b.id and b.visit-a.visit=1 order by id,visit;
quit;
data have;
input id $10. visit cycle $ date yymmdd10.;
format date yymmdd10. ;
cards;
100010001 1 CYCLE-1 2014-09-15
100010001 2 CYCLE-2 2014-10-27
100010001 3 CYCLE-3 2014-11-27
100010002 1 CYCLE-1 2014-10-13
100010002 2 CYCLE-2 2014-11-24
100010003 1 CYCLE-1 2015-02-08
100010003 1 CYCLE-1 2015-04-08
100010004 1 CYCLE-1 2015-04-28
100020001 1 CYCLE-1 2014-12-04
100020001 2 CYCLE-2 2015-01-15
100020001 3 CYCLE-3 2015-03-02
100020001 4 CYCLE-4 2015-04-13
100020002 1 CYCLE-1 2015-03-18
100020002 2 CYCLE-2 2015-04-29
100030002 1 CYCLE-1 2014-11-03
100030002 2 CYCLE-2 2014-12-22
100030002 3 CYCLE-3 2015-02-09
100030002 4 CYCLE-4 2015-03-23
100030003 1 CYCLE-1 2015-05-04
140010004 1 CYCLE-1 2015-02-11
140010004 2 CYCLE-2 2015-03-27
;
run;
proc sql;
select b.*,ifn(intck('week',a.date,b.date)>6,1,0) as flag from have a right join have b
on a.id=b.id and b.visit-a.visit=1 order by id,visit;
quit;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.