<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: separating out array elements in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/separating-out-array-elements/m-p/570507#M160877</link>
    <description>&lt;P&gt;If the number of columns changes, make the code data-driven:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 {&amp;amp;arrcount.} ARR:; /*gets the arrival times */
array DISCH {&amp;amp;arrcount.} DISCH:; /*gets the dicharge times*/
array RBC {&amp;amp;rbccount.} RBC:; /*gets the rbc timestamps*/
array CT {&amp;amp;rbccount.} CT:; /*gets the rbc timestamp counts*/

array rbci {&amp;amp;rbccount.} rbci1-rbci&amp;amp;rbccount.; /*used to get rbc timestamps for HGB later*/
array rbc0 {&amp;amp;rbccount.} rbc01-rbc0&amp;amp;rbccount.; /*used to get rbc timestamps for HGB later and if RBC falls outside hosp visits*/

array arrx {&amp;amp;arrcount.} arr1-arr&amp;amp;arrcount.;
array dtx {&amp;amp;arrcount.} DISCH1-DISCH&amp;amp;arrcount.;
array ctx {&amp;amp;rbccount.} ctx1-ctx&amp;amp;rbccount.;
array ctx0 {&amp;amp;rbccount.} ctx01-ctx0&amp;amp;rbccount.;

do i = 1 to dim(RBC);
  do j = 1 to dim(arr);
    if ARRx{j} &amp;lt; rbc{i} &amp;lt; 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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
    <pubDate>Tue, 02 Jul 2019 12:00:25 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-07-02T12:00:25Z</dc:date>
    <item>
      <title>separating out array elements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/separating-out-array-elements/m-p/570340#M160807</link>
      <description>&lt;P&gt;So the code below except in red (which I need help with)&amp;nbsp; &amp;nbsp;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)&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So for the example below&amp;nbsp;&lt;/P&gt;
&lt;P&gt;rbci1-rbc16 should be populated and then rbc07 should be populated and the relative ctx elements-&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;code below is snippet of larger code-&amp;nbsp; TIA in advance-&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have ; &lt;BR /&gt;input id:$1. (arr1 arr2 disch1 disch2 rbc1 rbc2 rbc3 rbc4 rbc5 rbc6 rbc7) (:datetime18.) ct1 ct2 ct3 ct4 ct5 ct6 ct7; &lt;BR /&gt;format arr: disch: rbc: datetime18. ; &lt;BR /&gt;cards ; &lt;BR /&gt;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 &lt;BR /&gt;run ;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;set have;&lt;/P&gt;
&lt;P&gt;array ARR (*) ARR:; /*gets the arrival times */&lt;BR /&gt;array DISCH(*) DISCH:; /*gets the dicharge times*/&lt;BR /&gt;array RBC (*) RBC:; /*gets the rbc timestamps*/&lt;BR /&gt;array CT (*) CT:; /*gets the rbc timestamp counts*/&lt;/P&gt;
&lt;P&gt;array rbci (7) rbci1-rbci7; /*used to get rbc timestamps for HGB later*/ &lt;BR /&gt;array rbc0 (7) rbc01-rbc07; /*used to get rbc timestamps for HGB later and if RBC falls outside hosp visits*/&lt;/P&gt;
&lt;P&gt;array arrx (3) arr1-arr3;&amp;nbsp;&lt;BR /&gt;array dtx (3) DISCH1-DISCH3;&amp;nbsp;&lt;BR /&gt;array ctx (7) ctx1-ctx7 ;&lt;BR /&gt;array ctx0 (7) ctx01-ctx07 ;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;do i=1 to DIM(RBC);&lt;BR /&gt;do j=1 to 3;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;if ARRx{j}&amp;lt;rbc(i)&amp;lt;Dtx{j} then do;&lt;/P&gt;
&lt;P&gt;rbci(i)=rbc(i);&lt;BR /&gt;ctx(i)=ct(i); &lt;BR /&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;if not ARRx{j}&amp;lt;rbc(i)&amp;lt;Dtx{j} then do;&amp;nbsp; &amp;nbsp; &amp;lt;----WHERE I&amp;nbsp; need help!&amp;nbsp;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;rbc0(i)=rbc(i);&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;ctx0(i)=ct(i); &lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;end;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;end;&lt;BR /&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;format rbci: rbc0: datetime19.;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jul 2019 18:38:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/separating-out-array-elements/m-p/570340#M160807</guid>
      <dc:creator>LB</dc:creator>
      <dc:date>2019-07-01T18:38:56Z</dc:date>
    </item>
    <item>
      <title>Re: separating out array elements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/separating-out-array-elements/m-p/570360#M160815</link>
      <description>&lt;P&gt;Why don't you do&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if ARRx{j} &amp;lt; rbc{i} &amp;lt; Dtx{j}
then do;
  rbci{i} = rbc{i};
  ctx{i} = ct{i}; 
end;
else do;
  rbc0{i} = rbc{i};
  ctx0{i} = ct{i}; 
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jul 2019 19:23:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/separating-out-array-elements/m-p/570360#M160815</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-01T19:23:39Z</dc:date>
    </item>
    <item>
      <title>Re: separating out array elements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/separating-out-array-elements/m-p/570364#M160816</link>
      <description>&lt;P&gt;I had tried that before- and it didn't work-&amp;nbsp;&lt;/P&gt;
&lt;P&gt;as the 2nd array elements also gets populated incorrectly&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best-&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Lawrence&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jul 2019 19:36:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/separating-out-array-elements/m-p/570364#M160816</guid>
      <dc:creator>LB</dc:creator>
      <dc:date>2019-07-01T19:36:39Z</dc:date>
    </item>
    <item>
      <title>Re: separating out array elements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/separating-out-array-elements/m-p/570385#M160822</link>
      <description>&lt;P&gt;Check your input data again. You have only two arrival and discharge times, but try to work with three.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jul 2019 20:27:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/separating-out-array-elements/m-p/570385#M160822</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-01T20:27:59Z</dc:date>
    </item>
    <item>
      <title>Re: separating out array elements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/separating-out-array-elements/m-p/570393#M160828</link>
      <description>&lt;P&gt;Good point-I changed the array elements to two- it seems to work for now.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However I have&amp;nbsp; to see what happens when I have pts with multiple admits/discharges and rbc transfers-&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jul 2019 21:26:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/separating-out-array-elements/m-p/570393#M160828</guid>
      <dc:creator>LB</dc:creator>
      <dc:date>2019-07-01T21:26:43Z</dc:date>
    </item>
    <item>
      <title>Re: separating out array elements</title>
      <link>https://communities.sas.com/t5/SAS-Programming/separating-out-array-elements/m-p/570507#M160877</link>
      <description>&lt;P&gt;If the number of columns changes, make the code data-driven:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 {&amp;amp;arrcount.} ARR:; /*gets the arrival times */
array DISCH {&amp;amp;arrcount.} DISCH:; /*gets the dicharge times*/
array RBC {&amp;amp;rbccount.} RBC:; /*gets the rbc timestamps*/
array CT {&amp;amp;rbccount.} CT:; /*gets the rbc timestamp counts*/

array rbci {&amp;amp;rbccount.} rbci1-rbci&amp;amp;rbccount.; /*used to get rbc timestamps for HGB later*/
array rbc0 {&amp;amp;rbccount.} rbc01-rbc0&amp;amp;rbccount.; /*used to get rbc timestamps for HGB later and if RBC falls outside hosp visits*/

array arrx {&amp;amp;arrcount.} arr1-arr&amp;amp;arrcount.;
array dtx {&amp;amp;arrcount.} DISCH1-DISCH&amp;amp;arrcount.;
array ctx {&amp;amp;rbccount.} ctx1-ctx&amp;amp;rbccount.;
array ctx0 {&amp;amp;rbccount.} ctx01-ctx0&amp;amp;rbccount.;

do i = 1 to dim(RBC);
  do j = 1 to dim(arr);
    if ARRx{j} &amp;lt; rbc{i} &amp;lt; 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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jul 2019 12:00:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/separating-out-array-elements/m-p/570507#M160877</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-02T12:00:25Z</dc:date>
    </item>
  </channel>
</rss>

