<?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: Array/Do Loop with Unknown Number of Iterations...? in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Array-Do-Loop-with-Unknown-Number-of-Iterations/m-p/239201#M6208</link>
    <description>&lt;P&gt;This looks great, exactly what I need. Thank you!&lt;/P&gt;</description>
    <pubDate>Mon, 14 Dec 2015 17:19:47 GMT</pubDate>
    <dc:creator>Matthew</dc:creator>
    <dc:date>2015-12-14T17:19:47Z</dc:date>
    <item>
      <title>Array/Do Loop with Unknown Number of Iterations...?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Array-Do-Loop-with-Unknown-Number-of-Iterations/m-p/239179#M6203</link>
      <description>&lt;P&gt;Hi all,&lt;BR /&gt;&lt;BR /&gt;I have a dataset that looks like this:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/1168iEB14E407E5140EA5/image-size/original?v=mpbl-1&amp;amp;px=-1" border="0" alt="data.JPG" title="data.JPG" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to get the information for each BUID into a single row. I know how to do this with arrays and do loops, but only when there's a fixed number of iterations, and I know what that number is. In this case, some BUIDs have more purchases than others for a given time period, and so the number of observations (and times the loop has the iterate) is variable.&lt;BR /&gt;&lt;BR /&gt;Currently my code looks like this:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data rfm162.fy13_p4_array2;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; retain&lt;BR /&gt;&amp;nbsp; &amp;nbsp; booked_date1-booked_date100&lt;BR /&gt;&amp;nbsp; &amp;nbsp; dmd1-dmd100&lt;BR /&gt;&amp;nbsp; &amp;nbsp; mbtwn1-mbtwn100&lt;BR /&gt;&amp;nbsp; &amp;nbsp; ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;rename count=order_count_fy13_p4 RNCY=rncy_today;&lt;BR /&gt;label RNCY='rncy_today';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;set rfm162.fy13_p4_array;&lt;BR /&gt;by curr_wg_buid booked_date;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;format booked_date1-booked_date100 datetime20.;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;array bd(100) booked_date1-booked_date100;&lt;BR /&gt;array money(100) dmd1-dmd100;&lt;BR /&gt;array mbtwn(100) mbtwn1-mbtwn100;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;do i=1 to 100;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; if i&amp;gt;count then do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; bd(i)=.;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; money(i)=.;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; mbtwn(i)=.;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; continue; &lt;BR /&gt;&amp;nbsp; &amp;nbsp; end;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; if i=count then do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; bd(i)=booked_date;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; money(i)=dmd;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; end;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if i&amp;gt;=2 then do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; m=i-1;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; mbtwn(i)=((bd(i)-bd(m))/2592000);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; end;&lt;BR /&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;dmd_average_fy13_p4=mean(of dmd1-dmd100);&lt;BR /&gt;dmd_total_fy13_p4=sum(of dmd1-dmd100);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;mbtwn_average_fy13_p4=mean(of mbtwn1-mbtwn100);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;drop dmd1-dmd100 mbtwn1-mbtwn100 dmd booked_date booked_date1-booked_date100 i m;&lt;BR /&gt;if last.curr_wg_buid;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code works, but it's messy in the sense that I might be cutting off a couple of transactions for some customers who made more than 100 transactions. Additionally, when I'm looking at a shorter time period, I've got to either do freq to figure out what the new max number of observations is, or make a relatively arbitrary guess. How can I get the do loop to figure out how many iterations it should loop for me in order to match the maximum number of observations for each BUID? I've got a feeling it has something to do with the *, based on my research, but I haven't been able to get that to work. I get weird "variable outside of array" type messages.&lt;BR /&gt;&lt;BR /&gt;Thanks,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2015 16:13:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Array-Do-Loop-with-Unknown-Number-of-Iterations/m-p/239179#M6203</guid>
      <dc:creator>Matthew</dc:creator>
      <dc:date>2015-12-14T16:13:51Z</dc:date>
    </item>
    <item>
      <title>Re: Array/Do Loop with Unknown Number of Iterations...?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Array-Do-Loop-with-Unknown-Number-of-Iterations/m-p/239183#M6204</link>
      <description>You'll have to scan your data to find the maximum since you do need to declare a length for the arrays.  You can store it in a macro variable and use that to declare your array lengths.&lt;BR /&gt;I would say, that's a inefficient/bad way to store your data, especially if you have 100 transactions per customer. &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;array bd(&amp;amp;array_size) booked_date1-booked_date&amp;amp;array_size;&lt;BR /&gt;array money(&amp;amp;array_size) dmd1-dmd&amp;amp;array_size;&lt;BR /&gt;array mbtwn(&amp;amp;array_size) mbtwn1-mbtwn&amp;amp;array_size;&lt;BR /&gt;</description>
      <pubDate>Mon, 14 Dec 2015 16:22:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Array-Do-Loop-with-Unknown-Number-of-Iterations/m-p/239183#M6204</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2015-12-14T16:22:31Z</dc:date>
    </item>
    <item>
      <title>Re: Array/Do Loop with Unknown Number of Iterations...?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Array-Do-Loop-with-Unknown-Number-of-Iterations/m-p/239184#M6205</link>
      <description>&lt;P&gt;Why not just use three transposes and merge the result back. &amp;nbsp;This is probably the simplest method as you don't need to know number of elements. &amp;nbsp;I posted about this in a very similar topic earlier:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/Base-SAS-Programming/sas-enquiry/m-p/239140#M43961" target="_blank"&gt;https://communities.sas.com/t5/Base-SAS-Programming/sas-enquiry/m-p/239140#M43961&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Otherwise, you can get the number of elements by:&lt;/P&gt;
&lt;PRE&gt;proc sql noprint;
  select max(element_number) 
  into  :max_elements
  from have;
quit;

data want;
  array a{&amp;amp;max_elements.} 8.;
  do i=1 to &amp;amp;max_elements.;
....&lt;/PRE&gt;
&lt;P&gt;Why the need to transpose? &amp;nbsp;Is it just for for an output file, if not then remain with the normalised structure, its far easier to program with.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2015 16:22:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Array-Do-Loop-with-Unknown-Number-of-Iterations/m-p/239184#M6205</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2015-12-14T16:22:59Z</dc:date>
    </item>
    <item>
      <title>Re: Array/Do Loop with Unknown Number of Iterations...?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Array-Do-Loop-with-Unknown-Number-of-Iterations/m-p/239198#M6206</link>
      <description>One more option - a macro written by users here:&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://www.sas.com/content/dam/SAS/en_ca/User%20Group%20Presentations/TASS/Tabachneck-FlipTranspose.pdf" target="_blank"&gt;http://www.sas.com/content/dam/SAS/en_ca/User%20Group%20Presentations/TASS/Tabachneck-FlipTranspose.pdf&lt;/A&gt;</description>
      <pubDate>Mon, 14 Dec 2015 17:01:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Array-Do-Loop-with-Unknown-Number-of-Iterations/m-p/239198#M6206</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2015-12-14T17:01:19Z</dc:date>
    </item>
    <item>
      <title>Re: Array/Do Loop with Unknown Number of Iterations...?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Array-Do-Loop-with-Unknown-Number-of-Iterations/m-p/239201#M6208</link>
      <description>&lt;P&gt;This looks great, exactly what I need. Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 14 Dec 2015 17:19:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Array-Do-Loop-with-Unknown-Number-of-Iterations/m-p/239201#M6208</guid>
      <dc:creator>Matthew</dc:creator>
      <dc:date>2015-12-14T17:19:47Z</dc:date>
    </item>
  </channel>
</rss>

