| SubjectID | FormDate | Status | Lag_formdate | interval_months | form_num |
| 1 | 3/24/2015 | 2 | . | . | 1 |
| 1 | 7/7/2015 | 3 | 3/24/2015 | 4 | 2 |
| 1 | 4/14/2016 | 3 | 7/7/2015 | 9 | 3 |
| 1 | 7/7/2016 | 3 | 4/14/2016 | 3 | 4 |
| 1 | 10/12/2016 | 3 | 7/7/2016 | 3 | 5 |
| 3 | 8/12/2015 | 3 | . | . | 1 |
| 3 | 11/17/2015 | 3 | 8/12/2015 | 3 | 2 |
| 3 | 4/5/2016 | 3 | 11/17/2015 | 5 | 3 |
| 3 | 5/12/2016 | 3 | 4/5/2016 | 1 | 4 |
| 5 | 1/16/2015 | 2 | . | . | 1 |
| 5 | 2/13/2015 | 2 | 1/16/2015 | 1 | 2 |
| 5 | 9/2/2015 | 2 | 2/13/2015 | 7 | 3 |
| 5 | 5/20/2016 | 3 | 9/2/2015 | 8 | 4 |
| 5 | 12/9/2016 | 3 | 5/20/2016 | 7 | 5 |
| 7 | 10/15/2015 | 1 | . | . | 1 |
| 7 | 4/14/2016 | 3 | 10/15/2015 | 6 | 2 |
| 7 | 10/27/2016 | 3 | 4/14/2016 | 6 | 3 |
| 8 | 4/6/2015 | 3 | . | . | 1 |
| 8 | 1/21/2016 | 3 | 4/6/2015 | 9 | 2 |
| 8 | 8/23/2016 | 3 | 1/21/2016 | 7 | 3 |
Dataset A is above.
I want to keep the first form per subject and the first form that comes <= 6 months; so there are 2 forms per client in the output dataset.
I have calculated the lag form date and presented in a column above. I would use the lag date minus the form date to get the first assessment after 6 months.
I need help getting the first form after 6 months.
data B;
set A;
by subjectID formdate;
if first.subjectID;
run;
Assuming I understand, kindly verify the code below
data have1;
input SubjectID FormDate :mmddyy10. Status Lag_formdate :mmddyy10. interval_months form_num;
format formdate Lag_formdate mmddyy10.;
cards;
1 3/24/2015 2 . . 1
1 7/7/2015 3 3/24/2015 4 2
1 4/14/2016 3 7/7/2015 9 3
1 7/7/2016 3 4/14/2016 3 4
1 10/12/2016 3 7/7/2016 3 5
3 8/12/2015 3 . . 1
3 11/17/2015 3 8/12/2015 3 2
3 4/5/2016 3 11/17/2015 5 3
3 5/12/2016 3 4/5/2016 1 4
5 1/16/2015 2 . . 1
5 2/13/2015 2 1/16/2015 1 2
5 9/2/2015 2 2/13/2015 7 3
5 5/20/2016 3 9/2/2015 8 4
5 12/9/2016 3 5/20/2016 7 5
7 10/15/2015 1 . . 1
7 4/14/2016 3 10/15/2015 6 2
7 10/27/2016 3 4/14/2016 6 3
8 4/6/2015 3 . . 1
8 1/21/2016 3 4/6/2015 9 2
8 8/23/2016 3 1/21/2016 7 3
;
data want;
set have1;
by SubjectID;
retain _t _f;
if first.SubjectID then do;
output;
_t=FormDate;
_f=0;
end;
else _k=intck('month',_t,FormDate);
if _k>=6 and not _f then do;
output;
_f=1;
end;
drop _:;
run;
Can you plz post the expected output for your input sample
| SubjectID | FormDate | Status | Lag_formdate | interval_months | form_num |
| 1 | 3/24/2015 | 2 | . | . | 1 |
| 1 | 4/14/2016 | 3 | 7/7/2015 | 9 | 3 |
| 3 | 8/12/2015 | 3 | . | . | 1 |
| 3 | 4/5/2016 | 3 | 11/17/2015 | 5 | 3 |
| 5 | 1/16/2015 | 2 | . | . | 1 |
| 5 | 9/2/2015 | 2 | 2/13/2015 | 7 | 3 |
| 7 | 10/15/2015 | 1 | . | . | 1 |
| 7 | 4/14/2016 | 3 | 10/15/2015 | 6 | 2 |
| 8 | 4/6/2015 | 3 | . | . | 1 |
| 8 | 1/21/2016 | 3 | 4/6/2015 | 9 | 2 |
I am not quite getting the 6 months interval in my head, can you explain how you derive that in your sample.
I beg your pardon, my brain is not functioning well today
The first form I want is the earliest form for a subject, and then I would take the next form that comes after 6 months. For example, say a subject has 3 forms entered on the following dates: 01/01/2018, 03/01/2018, and 08/01/2018. The first form would be dated 01/01/2018, and then the next form would be the one that comes right after a 6 month window, so the form entered on 08/01/2018. Make sense?
Assuming I understand, kindly verify the code below
data have1;
input SubjectID FormDate :mmddyy10. Status Lag_formdate :mmddyy10. interval_months form_num;
format formdate Lag_formdate mmddyy10.;
cards;
1 3/24/2015 2 . . 1
1 7/7/2015 3 3/24/2015 4 2
1 4/14/2016 3 7/7/2015 9 3
1 7/7/2016 3 4/14/2016 3 4
1 10/12/2016 3 7/7/2016 3 5
3 8/12/2015 3 . . 1
3 11/17/2015 3 8/12/2015 3 2
3 4/5/2016 3 11/17/2015 5 3
3 5/12/2016 3 4/5/2016 1 4
5 1/16/2015 2 . . 1
5 2/13/2015 2 1/16/2015 1 2
5 9/2/2015 2 2/13/2015 7 3
5 5/20/2016 3 9/2/2015 8 4
5 12/9/2016 3 5/20/2016 7 5
7 10/15/2015 1 . . 1
7 4/14/2016 3 10/15/2015 6 2
7 10/27/2016 3 4/14/2016 6 3
8 4/6/2015 3 . . 1
8 1/21/2016 3 4/6/2015 9 2
8 8/23/2016 3 1/21/2016 7 3
;
data want;
set have1;
by SubjectID;
retain _t _f;
if first.SubjectID then do;
output;
_t=FormDate;
_f=0;
end;
else _k=intck('month',_t,FormDate);
if _k>=6 and not _f then do;
output;
_f=1;
end;
drop _:;
run;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.