BookmarkSubscribeRSS Feed
LB
Quartz | Level 8 LB
Quartz | Level 8

So the code below except in red (which I need help with)   works -in essence I am attempting to separate array elements when pts receive a red blood cell transfusion in the hospital (rbci) vs if they receive one outside the hospital (rbc0) 

 

So for the example below 

rbci1-rbc16 should be populated and then rbc07 should be populated and the relative ctx elements-

 

code below is snippet of larger code-  TIA in advance- 

 

 

data have ;
input id:$1. (arr1 arr2 disch1 disch2 rbc1 rbc2 rbc3 rbc4 rbc5 rbc6 rbc7) (:datetime18.) ct1 ct2 ct3 ct4 ct5 ct6 ct7;
format arr: disch: rbc: datetime18. ;
cards ;
A 22Sep2010:16:17:00 01Oct2010:19:10:00 17DEC2010:14:53:00 22DEC2010:11:57:00 06NOV2010:11:58:00 11NOV2010:11:15:00 16NOV2010:13:24:00 16NOV2010:15:31:00 17NOV2010:13:27:00 19DEC2010:03:46:00 25DEC2010:07:44:00 1 1 1 1 1 1 1
run ;

data want;
set have;

array ARR (*) ARR:; /*gets the arrival times */
array DISCH(*) DISCH:; /*gets the dicharge times*/
array RBC (*) RBC:; /*gets the rbc timestamps*/
array CT (*) CT:; /*gets the rbc timestamp counts*/

array rbci (7) rbci1-rbci7; /*used to get rbc timestamps for HGB later*/
array rbc0 (7) rbc01-rbc07; /*used to get rbc timestamps for HGB later and if RBC falls outside hosp visits*/

array arrx (3) arr1-arr3; 
array dtx (3) DISCH1-DISCH3; 
array ctx (7) ctx1-ctx7 ;
array ctx0 (7) ctx01-ctx07 ;


do i=1 to DIM(RBC);
do j=1 to 3;


if ARRx{j}<rbc(i)<Dtx{j} then do;

rbci(i)=rbc(i);
ctx(i)=ct(i);
end;

 

if not ARRx{j}<rbc(i)<Dtx{j} then do;    <----WHERE I  need help! 

rbc0(i)=rbc(i);
ctx0(i)=ct(i);
end;

end;
end;

 


format rbci: rbc0: datetime19.;
run;

 

 

5 REPLIES 5
Kurt_Bremser
Super User

Why don't you do

if ARRx{j} < rbc{i} < Dtx{j}
then do;
  rbci{i} = rbc{i};
  ctx{i} = ct{i}; 
end;
else do;
  rbc0{i} = rbc{i};
  ctx0{i} = ct{i}; 
end;

?

LB
Quartz | Level 8 LB
Quartz | Level 8

I had tried that before- and it didn't work- 

as the 2nd array elements also gets populated incorrectly

 

Best- 

 

Lawrence

 

 

 

LB
Quartz | Level 8 LB
Quartz | Level 8

Good point-I changed the array elements to two- it seems to work for now.  

However I have  to see what happens when I have pts with multiple admits/discharges and rbc transfers-

 

 

 

Kurt_Bremser
Super User

If the number of columns changes, make the code data-driven:

data have ; 
input id:$1. (arr1 arr2 disch1 disch2 rbc1 rbc2 rbc3 rbc4 rbc5 rbc6 rbc7) (:datetime18.) ct1 ct2 ct3 ct4 ct5 ct6 ct7; 
format arr: disch: rbc: datetime18. ; 
cards ; 
A 22Sep2010:16:17:00 01Oct2010:19:10:00 17DEC2010:14:53:00 22DEC2010:11:57:00 06NOV2010:11:58:00 11NOV2010:11:15:00 16NOV2010:13:24:00 16NOV2010:15:31:00 17NOV2010:13:27:00 19DEC2010:03:46:00 25DEC2010:07:44:00 1 1 1 1 1 1 1 
;

proc sql noprint;
select count(*) into :arrcount trimmed from dictionary.columns
where libname = 'WORK' and memname = 'HAVE' and upcase(name) like 'ARR%';
select count(*) into :rbccount trimmed from dictionary.columns
where libname = 'WORK' and memname = 'HAVE' and upcase(name) like 'RBC%';
quit;

data want;
set have;

array ARR {&arrcount.} ARR:; /*gets the arrival times */
array DISCH {&arrcount.} DISCH:; /*gets the dicharge times*/
array RBC {&rbccount.} RBC:; /*gets the rbc timestamps*/
array CT {&rbccount.} CT:; /*gets the rbc timestamp counts*/

array rbci {&rbccount.} rbci1-rbci&rbccount.; /*used to get rbc timestamps for HGB later*/
array rbc0 {&rbccount.} rbc01-rbc0&rbccount.; /*used to get rbc timestamps for HGB later and if RBC falls outside hosp visits*/

array arrx {&arrcount.} arr1-arr&arrcount.;
array dtx {&arrcount.} DISCH1-DISCH&arrcount.;
array ctx {&rbccount.} ctx1-ctx&rbccount.;
array ctx0 {&rbccount.} ctx01-ctx0&rbccount.;

do i = 1 to dim(RBC);
  do j = 1 to dim(arr);
    if ARRx{j} < rbc{i} < Dtx{j}
    then do;
      rbci{i} = rbc{i};
      ctx{i} = ct{i};
    end;
    else do;
      rbc0{i} = rbc{i};
      ctx0{i} = ct{i};
    end;
  end;
end;
format rbci: rbc0: datetime19.;
run;

But in real life, I'd rather transpose the initial dataset into two long datasets (one containing the smaller, the other the larger "array"), and use SQL to build a cartesian join between them. That way you could prevent the many missing values in the results by only outputting the matches.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 5 replies
  • 1379 views
  • 0 likes
  • 2 in conversation