## Arrays-using multiple variables against other multiple events

Solved
Regular Contributor
Posts: 159

# Arrays-using multiple variables against other multiple events

Hi all;

Kinda stumped on this and I'll see if I can explain it well

What I have are patient encounters with multiple catheter insertions and/or multiple surgeries-

So a patient encounter may be

PT_ENC_NUMBER CATH_TIME1             CATH_TIME2           SURG_START1      SURG_START2     SURG_END1          SURG_END2

123                       01JAN17:12:00:00     05JAN17:12:00:00  01JAN17:10:00:00  07JAN17:13:00:00 01JAN17:13:00:00 07JAN17:17:00

So here Cath 1 was inserted in surg 1 but not Surgery2

Cath2 was not inserted in surgery1 or surgery2. So I know the total number of surgical insertions was 1-

All in all, its dynamic but let's say a patient can have up to 30 surgeries and 8 catheter insertions

I think I need a multi-dimension array but having some challenges overall;

Lawrence

Accepted Solutions
Solution
‎12-11-2017 03:42 PM
Super User
Posts: 13,293

## Re: Arrays-using multiple variables against other multiple events

You don't really show any desired result or explained it very clearly so I'll be guessing here.

I suspect that you want 3 separate arrays for the comparisons, one that would be the catheter times and the second the surgery start and the last the surgery end_end times.

Depending upon the desired result something like this may get you started. This assumes none of the surgery start/end pairs overlap and that the insertion occurs within a single interval and that all of your datetime variables are SAS valued datetimes. If they are character this won't work.

```data example;
set have;
array c cath_time: ;
array ss surg_start: ;
array se surg_end;
array ws {8}; /* this willl hold which surgery had an insertion number MUST
match number of cath_time variables*/
do 1 = 1 to dim(ss);
do j= 1 to dim(c);
if ss[i] le c[j] le se[i] then do;
/* found an insertion in the ith surgery for the jth cath*/
ws [j] = i;
end;
end;
End;
/* if you want a total number of cath insertions*/
NumInsert = n(of ws(*));

run;```

All Replies
SAS Employee
Posts: 24

## Re: Arrays-using multiple variables against other multiple events

Hi Lawrence,

My answer assumes that you have the start and end time for every surgery the patient had (aka. matching surg_startx and surg_endx).

``````data temp;
input PT_ENC_NUMBER @@;
input (CATH_TIME1 CATH_TIME2 SURG_START1 SURG_START2 SURG_END1 SURG_END2) ( :datetime.);
format CATH_TIME1 CATH_TIME2 SURG_START1 SURG_START2 SURG_END1 SURG_END2 datetime.;
datalines;
123 01JAN17:12:00:00 05JAN17:12:00:00 01JAN17:10:00:00 07JAN17:13:00:00 01JAN17:13:00:00 07JAN17:17:00
124 10JAN17:12:00:00 15JAN17:12:00:00 01JAN17:10:00:00 07JAN17:13:00:00 01JAN17:13:00:00 07JAN17:17:00
124 01JAN17:12:00:00 07JAN17:15:00:00 01JAN17:10:00:00 07JAN17:13:00:00 01JAN17:13:00:00 07JAN17:17:00
;

run;

data solution;
set temp;

array caths  [*] cath:       ;
array surg_s [*] surg_start: ;
array surg_e [*] surg_end:   ;

total=0;
do i = 1 to dim(caths);
do j = 1 to dim(surg_s);
if surg_s[j]<=caths[i]<=surg_e[j] then total+1;
end;
end;
run;``````

Solution
‎12-11-2017 03:42 PM
Super User
Posts: 13,293

## Re: Arrays-using multiple variables against other multiple events

You don't really show any desired result or explained it very clearly so I'll be guessing here.

I suspect that you want 3 separate arrays for the comparisons, one that would be the catheter times and the second the surgery start and the last the surgery end_end times.

Depending upon the desired result something like this may get you started. This assumes none of the surgery start/end pairs overlap and that the insertion occurs within a single interval and that all of your datetime variables are SAS valued datetimes. If they are character this won't work.

```data example;
set have;
array c cath_time: ;
array ss surg_start: ;
array se surg_end;
array ws {8}; /* this willl hold which surgery had an insertion number MUST
match number of cath_time variables*/
do 1 = 1 to dim(ss);
do j= 1 to dim(c);
if ss[i] le c[j] le se[i] then do;
/* found an insertion in the ith surgery for the jth cath*/
ws [j] = i;
end;
end;
End;
/* if you want a total number of cath insertions*/
NumInsert = n(of ws(*));

run;```
Regular Contributor
Posts: 159

## Re: Arrays-using multiple variables against other multiple events

Ballard;

As I said I having some trouble articulating what the results I was trying to get but you nailed 99.9% of the way there. I had to make a minor change but saved me a lot of hours spinning my wheels!

Thanks again-I love this forum!

Lawrence

Regular Contributor
Posts: 159

## Re: Arrays-using multiple variables against other multiple events

Ok Ballardw I am back with a follow up question-

So I have expanded the array a bit and everything works pretty well except for the components in red. I can tell by manually comparing the data.

The expanded version not just looks if the catheter was placed during a surgery but also there is a binary flag to whether that surgery was under 4 hours.  SO now I need surgeries whether catheter was placed in OR and another array to tell me if catheter was placed in OR and surgery was <4 hours. The array that calculates  whether it was done in surgery works great (Thank you again)

I have tried different combinations to no avail.

data hairyarray;
retain cath_day SURG SURG_INSERT POD surg_under4hr SURG_INSERT4HR ED_STAY INPT_INSERTION;
merge toarray final_cath_transpo final_surg_transpo final_cath_transpo_placement;
by pat_enc_csn_id;

array c START_DT: ; /*ARRAY FOR THE CATH START TIMES*/
array ce END_DT: ; /*ARRAY FOR THE CATH END TIMES*/
array ss LOG_PAT_IN_ROOM_TIME: ; /*ARRAY FOR THE SURG START TIMES*/
array se LOG_PAT_OUT_OF_ROOM_TIME: ; /*ARRAY FOR SURG END TIMES*/
array Surgfour surg_under4hr: ; /*ARRAY FOR THE SURG FOR UNDER 4 HOURS*/

array ws {&finalcath} ; /*ARRAY TO DETERMINE IF CATH PLACED IN OR*/
array podx {&finalcath} ; /*ARRAY TO DETERMINE IF CATH REMOVED POD1*/
array surgx{&finalsurgc} ;/*ARRAY TO COUNTS NUMBER OF SURGERIES*/
array SURGfouri{&finalcath} ;/*ARRAY TO DETERMINE IF CATH PLACED IN OR AND SURG UNDER 4 HOURS*/

do i = 1 to dim(ss);

do j= 1 to dim(c);
if ss[i] le c[j] le se[i] then do;

ws [j] = i;
if ws [j] = &finalsurgc then ws [j]=.; /*DELETES INCORRECT ENTRIES*/
if ws [j]=i and  Surgfour[i]^=. then SURGfouri[j]=i;
if ws [j]^=. and intck('dtday', se[i],ce[j])<2 then podx[j]=i; /*THIS GETS WHETHER IT WAS POD1 OR NO*/
if podx[j]=&finalsurgc-1 then podx[j]=.; /*DELETES INCORRECT ENTRIES*/

end;

if ss[i] ^=. then surgx[i]=1; /*COUNTS THE SURGERIES*/
if SURGfouri[j]>n(of surgx(*)) then SURGfouri[j]=.;
end;
end;

; /*TOTALS NUMBEROF SURGICAL INSERTIONS*/
POD = n(of PODx(*)); /*TOTALS NUMBER OF POD1 REMOVALS*/
SURG=n(of surgx(*)); /*TOTALS NUMBER OF SURGERIES*/
SURG_INSERT = n(of ws(*)); /*TOTALS NUMBER OF SURGICAL INSERTIONS*/
SURG_INSERT4HR=n(of SURGfouri(*)); /*TOTALS NUMBER OF SURGICAL INSERTIONS <4 HRS*/

run;

Lawrence

(ps If I am not making sense I apologize it's late)

☑ This topic is solved.