BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Bksong
Calcite | Level 5
how to calulate follow up years?

ID First visit(SAS date). Last visit(SAS date)
Id firstvisit lastvisit
01 5543

01 11234

02 5436

02 8895
Numbers are SAS date.

How to clalulate lastvisit- firstvisit?
It's difficult to calculate between first visit and last visit since located in different row.

How can make code correctly??


BK.

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

data have;
input id $ first_date last_date;
cards;
00001 5534 .
00001 . 10342
00002 4567 .
00002 . .
00002 .  7800
;

data want;
 do _n_=1 by 1 until(last.id);
  update have(obs=0) have;
  by id;
 end;
 do _n_=1 to _n_;
  followup_date=last_date-first_date;
  output;
 end;
run;

proc print noobs;run;
id first_date last_date followup_date
00001 5534 10342 4808
00001 5534 10342 4808
00002 4567 7800 3233
00002 4567 7800 3233
00002 4567 7800 3233

 

View solution in original post

7 REPLIES 7
Bksong
Calcite | Level 5
5543 is first visit
11234 is last visit
5436 is first visit
8895 is last visit
novinosrin
Tourmaline | Level 20

HI @Bksong  Since you mentioned first and last visit and that your visit dates are nicely sorted in ascending order within a given ID, all you need in my opinion is range function either in SQL, Proc means/summary

 

data have;
input Id $ visit;
format visit date9.;
cards;
01 5543
01 11234
02 5436
02 8895
;

proc sql;
 create table want as 
 select id, range(visit) as diff
 from have
 group by id;
quit;

  

Bksong
Calcite | Level 5
If visit date located in different calumn, how to make code?
novinosrin
Tourmaline | Level 20

Can you please post your "input" sample just like I posted the sample data have; and your expected output? plz

Bksong
Calcite | Level 5
idnumber first_date last_date
00001 5534
00001 . . . . . 10342
00002 4567
00002
00002 . . . . . 7800


Output
Idnumber first_date last_date followup_date
00001 5534 10342 4808
00001 5534 10342 4808
00002 4567 7890 3323
00002 4567 7890 3323
00002 4567 7890 3323
novinosrin
Tourmaline | Level 20

data have;
input id $ first_date last_date;
cards;
00001 5534 .
00001 . 10342
00002 4567 .
00002 . .
00002 .  7800
;

data want;
 do _n_=1 by 1 until(last.id);
  update have(obs=0) have;
  by id;
 end;
 do _n_=1 to _n_;
  followup_date=last_date-first_date;
  output;
 end;
run;

proc print noobs;run;
id first_date last_date followup_date
00001 5534 10342 4808
00001 5534 10342 4808
00002 4567 7800 3233
00002 4567 7800 3233
00002 4567 7800 3233

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 7 replies
  • 1629 views
  • 2 likes
  • 2 in conversation