BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
rakeshvvv
Quartz | Level 8

I have dataset with variables ID, VISITNUM, DATE, DAYS

I should write a query in such a way that for every ID, the difference in days should not be more then 49 between two visits.

100020001           1              2014-12-04          43

100020001           2              2015-01-15          85

100020002           1              2015-03-18          43

100020002           2              2015-04-29          85

100030002           1              2014-11-03          43

100030002           2              2014-12-22          92

100030002           3              2015-02-09          141

100030002           4              2015-03-23          183

100030003           1              2015-05-04          41

140010004           1              2015-02-11          28

140010004           2              2015-03-27          82

140020001           1              2014-09-22          43

140020001           2              2014-11-02          84

140020001           3              2014-12-21          133

140020001           4              2015-02-09          183

140020001           5              2015-03-23          225

My output should look like,

ID                          NEWVARIABLE

140020001,        2015-02-09; 2014-12-21;DIFF=50

140010004     2015-03-27; 2015-02-11;DIFF=54

Thanks everyone for great help.

Rakesh

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input ID : $20. VISITNUM DATE : yymmdd10. DAYS ;
format      DATE  yymmdd10.;
cards;
100020001           1              2014-12-04          43
100020001           2              2015-01-15          85
100020002           1              2015-03-18          43
100020002           2              2015-04-29          85
100030002           1              2014-11-03          43
100030002           2              2014-12-22          92
100030002           3              2015-02-09          141
100030002           4              2015-03-23          183
100030003           1              2015-05-04          41
140010004           1              2015-02-11          28
140010004           2              2015-03-27          82
140020001           1              2014-09-22          43
140020001           2              2014-11-02          84
140020001           3              2014-12-21          133
140020001           4              2015-02-09          183
140020001           5              2015-03-23          225
;
run;
data want;
 set have;
 length new $ 40;
 lag_date=lag(date);
 dif=dif(DAYS);     
 if id=lag(id) and dif gt 49 then do; new=cats(vvalue(lag_date),';',vvalue(date),';'); output;end;
 keep id new;
 format lag_date yymmdd10.;
 run;
 


Xia Keshan

View solution in original post

2 REPLIES 2
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

There are many posts on this type of subject on here, for example:

Ksharp
Super User
data have;
input ID : $20. VISITNUM DATE : yymmdd10. DAYS ;
format      DATE  yymmdd10.;
cards;
100020001           1              2014-12-04          43
100020001           2              2015-01-15          85
100020002           1              2015-03-18          43
100020002           2              2015-04-29          85
100030002           1              2014-11-03          43
100030002           2              2014-12-22          92
100030002           3              2015-02-09          141
100030002           4              2015-03-23          183
100030003           1              2015-05-04          41
140010004           1              2015-02-11          28
140010004           2              2015-03-27          82
140020001           1              2014-09-22          43
140020001           2              2014-11-02          84
140020001           3              2014-12-21          133
140020001           4              2015-02-09          183
140020001           5              2015-03-23          225
;
run;
data want;
 set have;
 length new $ 40;
 lag_date=lag(date);
 dif=dif(DAYS);     
 if id=lag(id) and dif gt 49 then do; new=cats(vvalue(lag_date),';',vvalue(date),';'); output;end;
 keep id new;
 format lag_date yymmdd10.;
 run;
 


Xia Keshan

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1087 views
  • 0 likes
  • 3 in conversation