<?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: compute cumulative product by id in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/compute-cumulative-product-by-id/m-p/325872#M72520</link>
    <description>&lt;P&gt;Thank you LaurieF for your suggestion, I just update my desired outputs.&lt;/P&gt;</description>
    <pubDate>Thu, 19 Jan 2017 04:43:54 GMT</pubDate>
    <dc:creator>Jonate_H</dc:creator>
    <dc:date>2017-01-19T04:43:54Z</dc:date>
    <item>
      <title>compute cumulative product by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compute-cumulative-product-by-id/m-p/325868#M72517</link>
      <description>&lt;P&gt;I am trying to&amp;nbsp;compute cumulative product for variable y, x, z by id, how to make following code works properly? thanks!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the following code computes correctly for&amp;nbsp;id=aa, but is wrong for id=bb. I would expect that computation for id=bb starts from bb's first observation, rather than continue from aa's last observation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $2. month y x z;
datalines;
aa 1 0.5 0.1 0.8
aa 2 3 3 3
aa 3 2 2 2
aa 4 3 3 3
aa 5 2 2 2
aa 6 1 1 1
aa 7 2 2 2
aa 8 2 2 2
aa 9 2 2 2
aa 10 3 3 3
bb 1 5 1 8
bb 2 3 3 3
bb 3 2 2 2
bb 4 3 3 3
bb 5 2 2 2
bb 6 1 1 1
bb 7 2 2 2
bb 8 2 2 2
bb 9 2 2 2
bb 10 3 3 3
;
run;

data want;
set have;
by id;
retain yproduct 1 xproduct 1 zproduct 1;
yproduct = yproduct*y;
xproduct = xproduct*x;
zproduct = zproduct*z;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Desired output looks like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/6792i0D9C5865F6036F53/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="output.jpg" title="output.jpg" /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 04:41:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compute-cumulative-product-by-id/m-p/325868#M72517</guid>
      <dc:creator>Jonate_H</dc:creator>
      <dc:date>2017-01-19T04:41:28Z</dc:date>
    </item>
    <item>
      <title>Re: compute cumulative product by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compute-cumulative-product-by-id/m-p/325870#M72519</link>
      <description>&lt;P&gt;Assuming you just want one row per group (2!), is this it?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by id;
array products[*] yproduct xproduct zproduct;
retain yproduct xproduct zproduct;
if first.id then do i = 1 to dim(products);  /* Initialise products at start of id */
   products[i] = 1;
   end;
yproduct = yproduct * y;
xproduct = xproduct * x;
zproduct = zproduct * z;
if last.id;                            /* Only output the last row for each id */
keep id yproduct xproduct zproduct;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Jan 2017 04:31:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compute-cumulative-product-by-id/m-p/325870#M72519</guid>
      <dc:creator>LaurieF</dc:creator>
      <dc:date>2017-01-19T04:31:34Z</dc:date>
    </item>
    <item>
      <title>Re: compute cumulative product by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compute-cumulative-product-by-id/m-p/325872#M72520</link>
      <description>&lt;P&gt;Thank you LaurieF for your suggestion, I just update my desired outputs.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 04:43:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compute-cumulative-product-by-id/m-p/325872#M72520</guid>
      <dc:creator>Jonate_H</dc:creator>
      <dc:date>2017-01-19T04:43:54Z</dc:date>
    </item>
    <item>
      <title>Re: compute cumulative product by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compute-cumulative-product-by-id/m-p/325874#M72522</link>
      <description>&lt;P&gt;Thank you - it&amp;nbsp;&lt;EM&gt;is&lt;/EM&gt; easier if we know what we're aiming at!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code is simpler - no&amp;nbsp;&lt;EM&gt;if last.id&lt;/EM&gt; and a modification to the&amp;nbsp;&lt;EM&gt;keep&lt;/EM&gt; statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by id;
array products[*] yproduct xproduct zproduct;
retain yproduct xproduct zproduct;
if first.id then do i = 1 to dim(products);  /* Initialise products at start of id */
   products[i] = 1;
   end;
yproduct = yproduct * y;
xproduct = xproduct * x;
zproduct = zproduct * z;
keep id x y x yproduct xproduct zproduct;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Jan 2017 04:47:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compute-cumulative-product-by-id/m-p/325874#M72522</guid>
      <dc:creator>LaurieF</dc:creator>
      <dc:date>2017-01-19T04:47:48Z</dc:date>
    </item>
    <item>
      <title>Re: compute cumulative product by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compute-cumulative-product-by-id/m-p/325879#M72524</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Thank you LaurieF!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 05:39:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compute-cumulative-product-by-id/m-p/325879#M72524</guid>
      <dc:creator>Jonate_H</dc:creator>
      <dc:date>2017-01-19T05:39:31Z</dc:date>
    </item>
    <item>
      <title>Re: compute cumulative product by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compute-cumulative-product-by-id/m-p/325880#M72525</link>
      <description>&lt;P&gt;It was a pleasure.&amp;nbsp;&lt;U&gt;&lt;EM&gt;&lt;/EM&gt;&lt;/U&gt;&lt;EM&gt;by&lt;/EM&gt;-group processing is one of the most powerful components of a data step. Other languages handle the same concept differently (or not at all!), but SAS's method is so elegant.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 05:41:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compute-cumulative-product-by-id/m-p/325880#M72525</guid>
      <dc:creator>LaurieF</dc:creator>
      <dc:date>2017-01-19T05:41:36Z</dc:date>
    </item>
  </channel>
</rss>

