<?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: Calculating returns from prices in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculating-returns-from-prices/m-p/724984#M225119</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/360730"&gt;@georgel&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It seems that you are dealing with data from CRSP or Compustat in Finance&lt;/P&gt;
&lt;P&gt;I agree with solution from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;, just want to highlight that you should calculate the return first then set the if later like this answer (because the way "if" plays quite tricky).&lt;/P&gt;
&lt;P&gt;You may receive&amp;nbsp; a lot of notes that "Missing ..." in your log, but it makes sense and (maybe) safe because there should be some missing obs somewhere in your dataset. If you want to be over this note, the norm is to set the full condition if you want to clarify any NOTE.&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;lag_prccd=lag(prccd);&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;may cause the note" missing..."&lt;/P&gt;
&lt;P&gt;but&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;lag_prccd=ifn(prccd&amp;lt;=0,., lag(prccd));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;won't result in such a note.&lt;/P&gt;
&lt;P&gt;Remember Compustat put the average of Bid/Ask spread as closing price (prccd in your case) and missing prccd will be set as 0 rather than ".", so be careful when dealing with such a variable in your dataset&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Warm regards,&lt;/P&gt;
&lt;P&gt;Phil.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 09 Mar 2021 21:08:29 GMT</pubDate>
    <dc:creator>Phil_NZ</dc:creator>
    <dc:date>2021-03-09T21:08:29Z</dc:date>
    <item>
      <title>Calculating returns from prices</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-returns-from-prices/m-p/724964#M225107</link>
      <description>&lt;P&gt;Hi all&lt;/P&gt;
&lt;P&gt;Please need to simplify the following code or alternative more efficient way to do it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=example ;
     by ISIN datadate;
run;

data example ;
     set example;
	 lag_ISIN=lag(ISIN);
	 lag_PRCCD=lag(PRCCD);
	 If lag_ISIN=ISIN then RET=PRCCD/lag_PRCCD-1;else RET=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 09 Mar 2021 19:51:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-returns-from-prices/m-p/724964#M225107</guid>
      <dc:creator>georgel</dc:creator>
      <dc:date>2021-03-09T19:51:41Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating returns from prices</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-returns-from-prices/m-p/724967#M225109</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data example ;
     set example;
by ISIN;
	
RET=PRCCD/lag_PRCCD - 1;
if first.ISIN then RET=.;
format ret percent12.1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There really isn't much to simplify here that I can see, you still end up with roughly the same lines of code. You could possibly remove one by using IFN() but I don't think you gain any efficiency doing so.&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/360730"&gt;@georgel&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi all&lt;/P&gt;
&lt;P&gt;Please need to simplify the following code or alternative more efficient way to do it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=example ;
     by ISIN datadate;
run;

data example ;
     set example;
	 lag_ISIN=lag(ISIN);
	 lag_PRCCD=lag(PRCCD);
	 If lag_ISIN=ISIN then RET=PRCCD/lag_PRCCD-1;else RET=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Mar 2021 20:15:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-returns-from-prices/m-p/724967#M225109</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-03-09T20:15:06Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating returns from prices</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-returns-from-prices/m-p/724984#M225119</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/360730"&gt;@georgel&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It seems that you are dealing with data from CRSP or Compustat in Finance&lt;/P&gt;
&lt;P&gt;I agree with solution from&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;, just want to highlight that you should calculate the return first then set the if later like this answer (because the way "if" plays quite tricky).&lt;/P&gt;
&lt;P&gt;You may receive&amp;nbsp; a lot of notes that "Missing ..." in your log, but it makes sense and (maybe) safe because there should be some missing obs somewhere in your dataset. If you want to be over this note, the norm is to set the full condition if you want to clarify any NOTE.&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;lag_prccd=lag(prccd);&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;may cause the note" missing..."&lt;/P&gt;
&lt;P&gt;but&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;lag_prccd=ifn(prccd&amp;lt;=0,., lag(prccd));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;won't result in such a note.&lt;/P&gt;
&lt;P&gt;Remember Compustat put the average of Bid/Ask spread as closing price (prccd in your case) and missing prccd will be set as 0 rather than ".", so be careful when dealing with such a variable in your dataset&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Warm regards,&lt;/P&gt;
&lt;P&gt;Phil.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Mar 2021 21:08:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-returns-from-prices/m-p/724984#M225119</guid>
      <dc:creator>Phil_NZ</dc:creator>
      <dc:date>2021-03-09T21:08:29Z</dc:date>
    </item>
  </channel>
</rss>

