Dear SAS community,
I have two datasets. The first one is a log, which has the dates for all scheduled visits (i.e. V1, V2, V3, V4). The second is for an assessment that is done as a part of one of the visits, but is only done for one of the 4 visits and may not be done on the same day as the actual visit date.
I need to assign a visit type to the assessment form (V1 etc) based on which visit date the assessment date is closest to. I tried creating windows and assigning types that way, but it doesn't work well as there are varying lengths between each visit and between each assessment to the visit. Some ID's missed visits and some had repeat visits. The data structure is shown below.
1st data set - Log:
ID V1 V2 V3 V4 V1_extra V2_extra V3_extra v4_extra
1 01-Jun-15 15-Apr-16 23-Jun-17 . . 24-Jul-17 . .
2 10-Jan-13 08-Feb-14 15-Dec-14 20-May-17 11-Apr-13
3 08-May-14 09-May-15 . 22-May-17 . . . .
2nd data set -Assessment forms
ID visitdate visittype
1 12-Jun-16 xx
2 06-Mar-17 xx
3 10-Nov-16 xx
If there is an equal amount of time between two visits, I'd like to assign it to the earlier visit.
Below is some SAS code that I tried.
*join dates from log data set to form data set;
proc sql;
create table logforms
as select forms.*, log.v1, log.v2, log.v3, log.v4
from forms
left join log
on forms.id=log.id;
quit;
/*method 1*/
*assign each form with the visit it is closest to;
data logforms2;
set logforms;
*study visit-- within window;
if (visitdate <v1+180 and visitdate >v1-180) then visittype=1;
else if (visitdate <v2+180 and visitdate >v2-180) then visittype=2;
else if (visitdate <v3+180 and visitdate >v3-180) then visittype=3;
else if (visitdate <v4+180 and visitdate >v4-180) then visittype=4;
else if visitdate=v1_extra or visitdate=v2_extra then visittype=700; *some extra dates also listed on log for repeat visits;
else visittype=88; *not scheduled;
run;
*Also tried it this way;
/*method 2*/
*add visit types;
data formlog2;
set formlog;
*calculate time between each form and each visit;
v1t=abs(visitdate-v1);
v2t=abs(visitdate-v2);
v3t=abs(visitdate-v3);
v4t=abs(visitdate-v4);
run;
*calc which form has the smallest amount of time between it and each bonevisit;
proc sql;
create table form_vt
as select formlog2.*, min(v1t) as min_v1t, min(v2t) as min_v2t, min(v3t) as min_v3t,
min(v4t) as min_v4t
from formlog2
group by id;
quit;
*assign each form with the visit it is closest to (within 180 days);
data form_vt2;
set form_vt;
if (v1t le 180 and v1t=min_v1t and not missing(min_v1t)) then visittype=1;
else if(v2t le 180 and v2t=min_v1t and not missing(min_v2t)) then visittype=2;
else if(v3t le 180 and v3t=min_v1t and not missing(min_v3t)) then visittype=3;
else if(v4t le 180 and v4t=min_v1t and not missing(min_v4t)) then visittype=4;
else visittype=88; *unscheduled;
run;
Hi,
You can try this approach to identify the closest log date and then assign visit type accordingly;
1. Transpose the log dataset by ID and then combine with assessment dataset on ID
2. Calculate the absolute difference between the log date and assesslemt date(if dates are not numeric convert to numeric dates)
3. Sort the resultant dataset on ID, diffrence and log date
4. Select the first record per ID using first.id
Below is exapmle how the data would look like, one hypothetical case with equal distance is added.
ID Visit_log Visdate_log Visdate_assessment Visittype_assessment Diff
1 V2 15-Apr-16 12-Jun-16 XX 58
1 V_hypothetical 9-Aug-16 12-Jun-16 XX 58
1 V3 23-Jun-17 12-Jun-16 XX 376
1 V1 1-Jun-15 12-Jun-16 XX 377
1 V2 extra 24-Jul-17 12-Jun-16 XX 407
Snehal
Hi,
You can try this approach to identify the closest log date and then assign visit type accordingly;
1. Transpose the log dataset by ID and then combine with assessment dataset on ID
2. Calculate the absolute difference between the log date and assesslemt date(if dates are not numeric convert to numeric dates)
3. Sort the resultant dataset on ID, diffrence and log date
4. Select the first record per ID using first.id
Below is exapmle how the data would look like, one hypothetical case with equal distance is added.
ID Visit_log Visdate_log Visdate_assessment Visittype_assessment Diff
1 V2 15-Apr-16 12-Jun-16 XX 58
1 V_hypothetical 9-Aug-16 12-Jun-16 XX 58
1 V3 23-Jun-17 12-Jun-16 XX 376
1 V1 1-Jun-15 12-Jun-16 XX 377
1 V2 extra 24-Jul-17 12-Jun-16 XX 407
Snehal
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.