<?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: Multiplying observations-values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Multiplying-observations-values/m-p/765989#M242685</link>
    <description>&lt;P&gt;Thanks.&amp;nbsp; Does not work though.&amp;nbsp; The group year=1967 has one &lt;EM&gt;Returns&lt;/EM&gt; value=0, but &lt;EM&gt;newcol&lt;/EM&gt; has a non-zero value (1.027*1.026).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="rommel_0-1630716193454.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/63286i0C3687F9AE07B49A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="rommel_0-1630716193454.png" alt="rommel_0-1630716193454.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 04 Sep 2021 00:45:53 GMT</pubDate>
    <dc:creator>dataMart87</dc:creator>
    <dc:date>2021-09-04T00:45:53Z</dc:date>
    <item>
      <title>Multiplying observations-values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiplying-observations-values/m-p/765906#M242654</link>
      <description>&lt;P&gt;&lt;A href="https://communities.sas.com/t5/General-SAS-Programming/Multiplying-observations-values-in-row-1-by-values-in-row-2-and/td-p/363815" target="_blank"&gt;https://communities.sas.com/t5/General-SAS-Programming/Multiplying-observations-values-in-row-1-by-values-in-row-2-and/td-p/363815&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a follow question to the link above.&amp;nbsp; The solution works for non-zero values.&amp;nbsp; Is there a solution if one of values in the group is 0 (zero)?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;infile datalines dlm=',' dsd truncover;&lt;BR /&gt;input ID Date:anydtdte. Returns Delisting_return month year;&lt;BR /&gt;format date date9.;&lt;BR /&gt;datalines;&lt;BR /&gt;1,1967-10-28,0,,10,1967&lt;BR /&gt;1,1967-11-28,1.026,,11,1967&lt;BR /&gt;1,1967-12-28,1.027,,12,1967&lt;BR /&gt;1,1968-01-28,1.01,,1,1968&lt;BR /&gt;1,1968-02-28,1.04,,2,1968&lt;BR /&gt;1,1968-03-28,1.001,,3,1968&lt;BR /&gt;1,1968-04-28,1.005,,4,1968&lt;BR /&gt;1,1968-05-28,1.02,,5,1968&lt;BR /&gt;1,1968-06-28,0.02,,6,1968&lt;BR /&gt;1,1968-07-28,0.06,,7,1968&lt;BR /&gt;1,1968-08-28,0.06,,8,1968&lt;BR /&gt;1,1968-09-28,0.07,,9,1968&lt;BR /&gt;1,1968-10-28,0.07,,10,1968&lt;BR /&gt;1,1968-11-28,0.08,,11,1968&lt;BR /&gt;1,1968-12-28,0.01,,12,1968&lt;BR /&gt;1,1969-01-28,0.01,,1,1969&lt;BR /&gt;1,1969-02-28,0.04,,2,1969&lt;BR /&gt;1,1969-03-28,0.001,,3,1969&lt;BR /&gt;1,1969-04-28,0.005,,4,1969&lt;BR /&gt;; &lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#000080"&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt;&lt;/FONT&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="3" color="#000080"&gt;sql&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;create&lt;/FONT&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;table&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;want&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;as&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;select&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;year, returns, exp(sum(log(returns)))&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;as&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;newcol&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;from&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;have&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;group&lt;/FONT&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;FONT face="Courier New" size="3" color="#0000ff"&gt;by&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;year;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size="3" color="#000080"&gt;&lt;STRONG&gt;quit&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Sep 2021 17:44:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiplying-observations-values/m-p/765906#M242654</guid>
      <dc:creator>dataMart87</dc:creator>
      <dc:date>2021-09-03T17:44:28Z</dc:date>
    </item>
    <item>
      <title>Re: Multiplying observations-values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiplying-observations-values/m-p/765907#M242655</link>
      <description>LOG(0) is undefined so what do you want to return in that case? 0? Missing?&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 03 Sep 2021 17:46:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiplying-observations-values/m-p/765907#M242655</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-09-03T17:46:05Z</dc:date>
    </item>
    <item>
      <title>Re: Multiplying observations-values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiplying-observations-values/m-p/765908#M242656</link>
      <description>&lt;P&gt;I would like it to return a 0, just like multiplication by 0 returns a 0.&lt;/P&gt;</description>
      <pubDate>Fri, 03 Sep 2021 17:48:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiplying-observations-values/m-p/765908#M242656</guid>
      <dc:creator>dataMart87</dc:creator>
      <dc:date>2021-09-03T17:48:28Z</dc:date>
    </item>
    <item>
      <title>Re: Multiplying observations-values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiplying-observations-values/m-p/765916#M242662</link>
      <description>But log of 0 is undefined.....&lt;BR /&gt;&lt;BR /&gt;So then you want something like below I'd guess:&lt;BR /&gt;&lt;BR /&gt;exp(sum(case when returns = 0 then 0 else log(returns) end))&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 03 Sep 2021 19:30:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiplying-observations-values/m-p/765916#M242662</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-09-03T19:30:30Z</dc:date>
    </item>
    <item>
      <title>Re: Multiplying observations-values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiplying-observations-values/m-p/765989#M242685</link>
      <description>&lt;P&gt;Thanks.&amp;nbsp; Does not work though.&amp;nbsp; The group year=1967 has one &lt;EM&gt;Returns&lt;/EM&gt; value=0, but &lt;EM&gt;newcol&lt;/EM&gt; has a non-zero value (1.027*1.026).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="rommel_0-1630716193454.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/63286i0C3687F9AE07B49A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="rommel_0-1630716193454.png" alt="rommel_0-1630716193454.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 04 Sep 2021 00:45:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiplying-observations-values/m-p/765989#M242685</guid>
      <dc:creator>dataMart87</dc:creator>
      <dc:date>2021-09-04T00:45:53Z</dc:date>
    </item>
    <item>
      <title>Re: Multiplying observations-values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiplying-observations-values/m-p/766025#M242704</link>
      <description>It would be all 0 ,since 0 multiply anything is 0 .</description>
      <pubDate>Sat, 04 Sep 2021 11:33:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiplying-observations-values/m-p/766025#M242704</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-09-04T11:33:13Z</dc:date>
    </item>
    <item>
      <title>Re: Multiplying observations-values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiplying-observations-values/m-p/766032#M242707</link>
      <description>&lt;P&gt;If you have SAS/IML, could try PROD() function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
infile datalines dlm=',' dsd truncover;
input ID Date:anydtdte. Returns Delisting_return month year;
format date date9.;
datalines;
1,1967-10-28,0,,10,1967
1,1967-11-28,1.026,,11,1967
1,1967-12-28,1.027,,12,1967
1,1968-01-28,1.01,,1,1968
1,1968-02-28,1.04,,2,1968
1,1968-03-28,1.001,,3,1968
1,1968-04-28,1.005,,4,1968
1,1968-05-28,1.02,,5,1968
1,1968-06-28,0.02,,6,1968
1,1968-07-28,0.06,,7,1968
1,1968-08-28,0.06,,8,1968
1,1968-09-28,0.07,,9,1968
1,1968-10-28,0.07,,10,1968
1,1968-11-28,0.08,,11,1968
1,1968-12-28,0.01,,12,1968
1,1969-01-28,0.01,,1,1969
1,1969-02-28,0.04,,2,1969
1,1969-03-28,0.001,,3,1969
1,1969-04-28,0.005,,4,1969
;
run;

proc iml;
use have  nobs nobs;
read all var {year Returns};

first=uniqueby(year);
last=remove(first,1)-1||nobs;

product=j(nrow(first),1,.);
do i=1 to nrow(first);
 product[i]=prod(Returns[first[i]:last[i]]);
end;

print (year[first]) product;
quit;&lt;/PRE&gt;</description>
      <pubDate>Sat, 04 Sep 2021 12:01:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiplying-observations-values/m-p/766032#M242707</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-09-04T12:01:44Z</dc:date>
    </item>
    <item>
      <title>Re: Multiplying observations-values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiplying-observations-values/m-p/766034#M242708</link>
      <description>&lt;P&gt;Or try data step ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
infile datalines dlm=',' dsd truncover;
input ID Date:anydtdte. Returns Delisting_return month year;
format date date9.;
datalines;
1,1967-10-28,0,,10,1967
1,1967-11-28,1.026,,11,1967
1,1967-12-28,1.027,,12,1967
1,1968-01-28,1.01,,1,1968
1,1968-02-28,1.04,,2,1968
1,1968-03-28,1.001,,3,1968
1,1968-04-28,1.005,,4,1968
1,1968-05-28,1.02,,5,1968
1,1968-06-28,0.02,,6,1968
1,1968-07-28,0.06,,7,1968
1,1968-08-28,0.06,,8,1968
1,1968-09-28,0.07,,9,1968
1,1968-10-28,0.07,,10,1968
1,1968-11-28,0.08,,11,1968
1,1968-12-28,0.01,,12,1968
1,1969-01-28,0.01,,1,1969
1,1969-02-28,0.04,,2,1969
1,1969-03-28,0.001,,3,1969
1,1969-04-28,0.005,,4,1969
;
run;
data want;
 do until(last.year);
  set have;
  by id year;
  if first.year then want=1;
  want=want*Returns;
 end;
  do until(last.year);
  set have;
  by id year;
  output;
 end;
run;&lt;/PRE&gt;</description>
      <pubDate>Sat, 04 Sep 2021 12:07:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiplying-observations-values/m-p/766034#M242708</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-09-04T12:07:27Z</dc:date>
    </item>
    <item>
      <title>Re: Multiplying observations-values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiplying-observations-values/m-p/766035#M242709</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/2725"&gt;@dataMart87&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To fix the PROC SQL approach, I suggest this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select year, returns,
       min(returns~=0)*exp(sum(log(ifn(returns,returns,1)))) as newcol
from have
group by year;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This assumes that&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;FONT face="courier new,courier"&gt;returns&lt;/FONT&gt; are positive, zero or missing (i.e., not negative)&lt;/LI&gt;
&lt;LI&gt;there is no year with &lt;EM&gt;only&lt;/EM&gt; missing&amp;nbsp;&lt;FONT face="courier new,courier"&gt;returns&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;you want to compute the product of all non-missing &lt;FONT face="courier new,courier"&gt;returns&lt;/FONT&gt;.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;If condition 2 is possibly not met, this special case should be handled in a CASE expression (or another call of the IFN function), e.g., then setting &lt;FONT face="courier new,courier"&gt;newcol&lt;/FONT&gt; to missing:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select year, returns,
       case when n(returns) then min(returns~=0)*exp(sum(log(ifn(returns,returns,1))))
            else . /* i.e., in the special case "only missing returns" */
       end as newcol
from have
group by year;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that the IFN(...) expression (or an equivalent CASE expression) in the argument of the LOG function prevents unwanted notes about invalid or missing arguments in the SAS log (as long as no negative &lt;FONT face="courier new,courier"&gt;returns&lt;/FONT&gt; occur).&lt;/P&gt;</description>
      <pubDate>Sat, 04 Sep 2021 13:48:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiplying-observations-values/m-p/766035#M242709</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-09-04T13:48:19Z</dc:date>
    </item>
    <item>
      <title>Re: Multiplying observations-values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiplying-observations-values/m-p/767157#M243172</link>
      <description>&lt;P&gt;Don't have&amp;nbsp;&lt;SPAN&gt;SAS/IML, but DATA step works.&amp;nbsp; Thanks,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 11 Sep 2021 01:18:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiplying-observations-values/m-p/767157#M243172</guid>
      <dc:creator>dataMart87</dc:creator>
      <dc:date>2021-09-11T01:18:09Z</dc:date>
    </item>
    <item>
      <title>Re: Multiplying observations-values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiplying-observations-values/m-p/767158#M243173</link>
      <description>&lt;P&gt;This works too.&amp;nbsp; Thanks,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 11 Sep 2021 01:19:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiplying-observations-values/m-p/767158#M243173</guid>
      <dc:creator>dataMart87</dc:creator>
      <dc:date>2021-09-11T01:19:15Z</dc:date>
    </item>
  </channel>
</rss>

