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 |
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;
Can you please post your "input" sample just like I posted the sample data have; and your expected output? plz
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 |
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!
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.
Ready to level-up your skills? Choose your own adventure.