<?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: Looping with Arrays in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Looping-with-Arrays/m-p/431636#M106800</link>
    <description>&lt;P&gt;I can't open attachments, however a quick look at your&amp;nbsp; code tells me, your index variable i in do iterative loop will remain 1 for all iterations&amp;nbsp; until the&amp;nbsp; until expression is true as i doesn't seem to increment anywhere in the inside the loop&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, your by statement should follow set statement or in other words set statement should precede by statement&lt;/P&gt;</description>
    <pubDate>Sun, 28 Jan 2018 23:00:11 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-01-28T23:00:11Z</dc:date>
    <item>
      <title>Looping with Arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-with-Arrays/m-p/431634#M106799</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to create 4 columns, Qtr1-Qtr4, which sums up the total amount of orders per quarter for each customer by year. I am trying to do this with a looping array but I am having some difficulty. Please view my code below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data quarters (drop = i Year order_year);&lt;BR /&gt;by customer_ID;&lt;BR /&gt;set tut.order_fact;&lt;BR /&gt;array Qtr_array {4} Qtr1-Qtr4;&lt;BR /&gt;Order_Year = intck('YEAR', Order_date, today(), 'C');&lt;BR /&gt;Year = min(order_year);&lt;BR /&gt;do i = 1 until (year =&amp;gt; Order_year);&lt;BR /&gt;Qtr_array {i} = sum(total_retail_price);&lt;BR /&gt;Year = year + 1;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've attached the dataset so you can view it. Please let me know if I need to clarify anything.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Thanks for any help.&lt;/P&gt;&lt;P&gt;Regards.&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jan 2018 22:48:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-with-Arrays/m-p/431634#M106799</guid>
      <dc:creator>Scott86</dc:creator>
      <dc:date>2018-01-28T22:48:41Z</dc:date>
    </item>
    <item>
      <title>Re: Looping with Arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-with-Arrays/m-p/431636#M106800</link>
      <description>&lt;P&gt;I can't open attachments, however a quick look at your&amp;nbsp; code tells me, your index variable i in do iterative loop will remain 1 for all iterations&amp;nbsp; until the&amp;nbsp; until expression is true as i doesn't seem to increment anywhere in the inside the loop&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, your by statement should follow set statement or in other words set statement should precede by statement&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jan 2018 23:00:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-with-Arrays/m-p/431636#M106800</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-01-28T23:00:11Z</dc:date>
    </item>
    <item>
      <title>Re: Looping with Arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-with-Arrays/m-p/431638#M106801</link>
      <description>&lt;P&gt;You could try a simple approach:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=order out=_order;
by customer_id order_date;
run;

data _order1;
set _order;
year=year(order_date);
qtr=qtr(order_date);
run;

proc sql;
create table want as
select customer_id, year, qtr, sum(total_retail_price) as amount
from _order1
group by customer_id, year, qtr
order by customer_id,year,qtr;
quit;

proc transpose data=want out=final_want;
by customer_id year;
var amount;
id qtr;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 28 Jan 2018 23:51:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-with-Arrays/m-p/431638#M106801</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-01-28T23:51:49Z</dc:date>
    </item>
    <item>
      <title>Re: Looping with Arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-with-Arrays/m-p/431640#M106802</link>
      <description>&lt;P&gt;Thanks guys, I am trying to practice my array and looping programs because its a weak area of mine so would like to solve this using an array.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jan 2018 00:35:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-with-Arrays/m-p/431640#M106802</guid>
      <dc:creator>Scott86</dc:creator>
      <dc:date>2018-01-29T00:35:36Z</dc:date>
    </item>
    <item>
      <title>Re: Looping with Arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-with-Arrays/m-p/431641#M106803</link>
      <description>&lt;P&gt;I think that the following does what you want:&lt;/P&gt;
&lt;PRE&gt;proc summary data=tut.order_fact nway;
  var Total_Retail_Price;
  class customer_ID Order_Date;
  format Order_date yyq6.;
  output out=need (drop=_:) sum=Sum_Retail_Price;
run;


proc transpose data=need out=want (drop=_:) prefix=Total_Price_;
  var Sum_Retail_Price;
  by customer_ID;
  id Order_date;
  format Order_date yyq6.;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jan 2018 00:35:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-with-Arrays/m-p/431641#M106803</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2018-01-29T00:35:39Z</dc:date>
    </item>
    <item>
      <title>Re: Looping with Arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-with-Arrays/m-p/431643#M106805</link>
      <description>&lt;P&gt;If you're trying to practice arrays, try to write a data step to accomplish the same thing that my suggested proc summary does, then write another data step that does what my suggested proc summary does. Hint: first sort the file by Order_Date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jan 2018 00:46:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-with-Arrays/m-p/431643#M106805</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2018-01-29T00:46:09Z</dc:date>
    </item>
    <item>
      <title>Re: Looping with Arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-with-Arrays/m-p/431647#M106807</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Thanks &lt;STRONG&gt;&lt;STRIKE&gt;guys&lt;/STRIKE&gt;&lt;/STRONG&gt;, I am trying to practice my array and looping programs because its a weak area of mine so would like to solve this using an array.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Knowing when to use which approach to use when is something more important to learn.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Because your data is in a long form using PROC MEANS and TRANSPOSE is more efficient. If you use arrays things you need to account for:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Determining boundaries of each year/quarter&lt;/P&gt;
&lt;P&gt;2. Summarizing records first&lt;/P&gt;
&lt;P&gt;3. Retaining data across rows&lt;/P&gt;
&lt;P&gt;4. Accounting for the last record, if it's not a full year&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Arrays really don't work well for long data sets, since arrays in SAS are only shortcut references to variable names. In other languages they're very different.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/135820"&gt;@Scott86&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi everyone,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to create 4 columns, Qtr1-Qtr4, which sums up the total amount of orders per quarter for each customer by year. I am trying to do this with a looping array but I am having some difficulty. Please view my code below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data quarters (drop = i Year order_year);&lt;BR /&gt;by customer_ID;&lt;BR /&gt;set tut.order_fact;&lt;BR /&gt;array Qtr_array {4} Qtr1-Qtr4;&lt;BR /&gt;Order_Year = intck('YEAR', Order_date, today(), 'C');&lt;BR /&gt;Year = min(order_year);&lt;BR /&gt;do i = 1 until (year =&amp;gt; Order_year);&lt;BR /&gt;Qtr_array {i} = sum(total_retail_price);&lt;BR /&gt;Year = year + 1;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've attached the dataset so you can view it. Please let me know if I need to clarify anything.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Thanks for any help.&lt;/P&gt;
&lt;P&gt;Regards.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Jan 2018 01:13:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-with-Arrays/m-p/431647#M106807</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-01-29T01:13:00Z</dc:date>
    </item>
    <item>
      <title>Re: Looping with Arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-with-Arrays/m-p/431670#M106823</link>
      <description>&lt;P&gt;Building on &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;'s suggestion,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=order out=_order;
by customer_id order_date;
run;

data _order1 / view=_order1;
set _order;
year=year(order_date);
qtr=qtr(order_date);
run;

data quarters;
array q qtr1-qtr4;
do until (last.year);
    set _order1; by customer_id year;
    q{qtr} = sum(q{qtr}, total_retail_price);
    end;
keep customer_id year qtr1-qtr4;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Jan 2018 05:09:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-with-Arrays/m-p/431670#M106823</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-01-29T05:09:15Z</dc:date>
    </item>
    <item>
      <title>Re: Looping with Arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-with-Arrays/m-p/432613#M107160</link>
      <description>&lt;P&gt;Thanks guys,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I got kinda close but needed your loop array to complete it :(.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My code was:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data Qtrs;&lt;BR /&gt;set order1;&lt;BR /&gt;by customer_ID year;&lt;BR /&gt;array qtr_array {4} Qtr1-Qtr4;&lt;BR /&gt;do year = 2007 to 2011;&lt;BR /&gt;qtr_array {4} = sum(total_sales);&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do I set it so if there are no sales in the Qtr is shows as 0? also can I format within the array so Qtr are in Dollar9.?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 05:51:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-with-Arrays/m-p/432613#M107160</guid>
      <dc:creator>Scott86</dc:creator>
      <dc:date>2018-01-31T05:51:38Z</dc:date>
    </item>
    <item>
      <title>Re: Looping with Arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Looping-with-Arrays/m-p/432615#M107161</link>
      <description>&lt;P&gt;Replace last step with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data quarters;
array q qtr1-qtr4;
format qtr1-qtr4 dollar9.;

do i = 1 to 4; 
    q{i} = 0; 
    end;

do until (last.year);
    set _order1; by customer_id year;
    q{qtr} = q{qtr} + total_retail_price;
    end;

keep customer_id year qtr1-qtr4;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 31 Jan 2018 06:00:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Looping-with-Arrays/m-p/432615#M107161</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-01-31T06:00:40Z</dc:date>
    </item>
  </channel>
</rss>

