<?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 Retain and Lag in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retain-and-Lag/m-p/692426#M210951</link>
    <description>&lt;P&gt;I use historical prices to compute returns and cumulative returns as follows.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ko(drop=dummy);
	infile 'https://query1.finance.yahoo.com/v7/finance/download/ko?
period1=-9999999999&amp;amp;period2=9999999999' url firstobs=2 dsd truncover;
	input date yymmdd10. +1 (4*dummy)(:$1.) ko;
	return=ko/lag(ko);
run;

proc expand method=none out=ko;
	id date;
	convert return=cumulative/tout=(cuprod);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;And I wanted to shorten the code using &lt;CODE&gt;retain&lt;/CODE&gt; as follows but failed.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ko(drop=dummy);
	infile 'https://query1.finance.yahoo.com/v7/finance/download/ko?
period1=-9999999999&amp;amp;period2=9999999999' url firstobs=2 dsd truncover;
	input date yymmdd10. +1 (4*dummy)(:$1.) ko;
	retain cumulative 1;
	cumulative=cumulative*ko/lag(ko);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I found that &lt;CODE&gt;cumulative&lt;/CODE&gt; without &lt;CODE&gt;/lag(ko)&lt;/CODE&gt; recursively multiplies the historical &lt;CODE&gt;ko&lt;/CODE&gt; values but the &lt;CODE&gt;/lag(ko)&lt;/CODE&gt; makes everything missing. What am I doing wrong here?&lt;/P&gt;&lt;P&gt;Updated: This is not the best but what I found.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ko2(drop=dummy);
	infile 'https://query1.finance.yahoo.com/v7/finance/download/ko?
period1=-9999999999&amp;amp;period2=9999999999' url firstobs=2 dsd truncover;
	input date yymmdd10. +1 (4*dummy)(:$1.) ko;
	retain cumulative;
	cumulative=ifn(lag(ko)&amp;gt;.,cumulative*ko/lag(ko),1);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 19 Oct 2020 19:32:04 GMT</pubDate>
    <dc:creator>Junyong</dc:creator>
    <dc:date>2020-10-19T19:32:04Z</dc:date>
    <item>
      <title>Retain and Lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-and-Lag/m-p/692426#M210951</link>
      <description>&lt;P&gt;I use historical prices to compute returns and cumulative returns as follows.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ko(drop=dummy);
	infile 'https://query1.finance.yahoo.com/v7/finance/download/ko?
period1=-9999999999&amp;amp;period2=9999999999' url firstobs=2 dsd truncover;
	input date yymmdd10. +1 (4*dummy)(:$1.) ko;
	return=ko/lag(ko);
run;

proc expand method=none out=ko;
	id date;
	convert return=cumulative/tout=(cuprod);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;And I wanted to shorten the code using &lt;CODE&gt;retain&lt;/CODE&gt; as follows but failed.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ko(drop=dummy);
	infile 'https://query1.finance.yahoo.com/v7/finance/download/ko?
period1=-9999999999&amp;amp;period2=9999999999' url firstobs=2 dsd truncover;
	input date yymmdd10. +1 (4*dummy)(:$1.) ko;
	retain cumulative 1;
	cumulative=cumulative*ko/lag(ko);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I found that &lt;CODE&gt;cumulative&lt;/CODE&gt; without &lt;CODE&gt;/lag(ko)&lt;/CODE&gt; recursively multiplies the historical &lt;CODE&gt;ko&lt;/CODE&gt; values but the &lt;CODE&gt;/lag(ko)&lt;/CODE&gt; makes everything missing. What am I doing wrong here?&lt;/P&gt;&lt;P&gt;Updated: This is not the best but what I found.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ko2(drop=dummy);
	infile 'https://query1.finance.yahoo.com/v7/finance/download/ko?
period1=-9999999999&amp;amp;period2=9999999999' url firstobs=2 dsd truncover;
	input date yymmdd10. +1 (4*dummy)(:$1.) ko;
	retain cumulative;
	cumulative=ifn(lag(ko)&amp;gt;.,cumulative*ko/lag(ko),1);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 19 Oct 2020 19:32:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-and-Lag/m-p/692426#M210951</guid>
      <dc:creator>Junyong</dc:creator>
      <dc:date>2020-10-19T19:32:04Z</dc:date>
    </item>
    <item>
      <title>Re: Retain and Lag</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-and-Lag/m-p/692443#M210959</link>
      <description>&lt;P&gt;By combining the steps you ignored the function of PROC EXPAND&lt;/P&gt;
&lt;P&gt;and you got missing value because the variable - cumulative - was not calculated and is missing value.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Oct 2020 07:49:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-and-Lag/m-p/692443#M210959</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-10-19T07:49:20Z</dc:date>
    </item>
  </channel>
</rss>

