<?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: computing buys and sells in a portfolio in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/computing-buys-and-sells-in-a-portfolio/m-p/363305#M274880</link>
    <description>&lt;P&gt;Thank art297 and ChrisNZ, both of your solutions work..&lt;/P&gt;</description>
    <pubDate>Thu, 01 Jun 2017 03:56:06 GMT</pubDate>
    <dc:creator>Ramani</dc:creator>
    <dc:date>2017-06-01T03:56:06Z</dc:date>
    <item>
      <title>computing buys and sells in a portfolio</title>
      <link>https://communities.sas.com/t5/SAS-Programming/computing-buys-and-sells-in-a-portfolio/m-p/363294#M274876</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I'm trying to wrap my head around how to compute buys and sells by a mutual fund in a particular quarter.&lt;/P&gt;&lt;P&gt;My data looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Date &amp;nbsp; &amp;nbsp; Fund &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Stock &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Shares-held&lt;BR /&gt;&lt;BR /&gt;Jun-90 Fund A &amp;nbsp; &amp;nbsp; &amp;nbsp;Apple &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5000&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Microsoft &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5000&lt;BR /&gt;Sep-90 Fund A &amp;nbsp; &amp;nbsp; &amp;nbsp;Apple &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 6000&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Microsoft &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;4000&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Oracle &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2000&lt;BR /&gt;Dec-90 Fund A &amp;nbsp; &amp;nbsp; &amp;nbsp;Apple &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;6000&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Oracle &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2000&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Facebook &amp;nbsp; &amp;nbsp; &amp;nbsp; 2000&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm in need of an&amp;nbsp;output data set something like this&lt;/P&gt;&lt;P&gt;buy/Sell &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Change in shares&lt;BR /&gt;Jun-90 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Apple 5000&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Microsoft 5000&lt;BR /&gt;Sep-90 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Apple 1000&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Microsoft -1000&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Oracle 2000&lt;BR /&gt;Dec-90 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Microsoft -4000&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Facebook 2000&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I tried using some combinations of sorting, if statements and lag statements and cannot seem to get all the values I want.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;P&gt;-R&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 02:58:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/computing-buys-and-sells-in-a-portfolio/m-p/363294#M274876</guid>
      <dc:creator>Ramani</dc:creator>
      <dc:date>2017-06-01T02:58:19Z</dc:date>
    </item>
    <item>
      <title>Re: computing buys and sells in a portfolio</title>
      <link>https://communities.sas.com/t5/SAS-Programming/computing-buys-and-sells-in-a-portfolio/m-p/363299#M274877</link>
      <description>&lt;P&gt;Likes this (I'll let you handle the dates properly) ?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data HAVE(index=(A=(FUND STOCK DATE )));
input DATE $ FUND $ STOCK $10. SHARES_HELD;
cards;
Apr-90 FundA Apple            5000
Apr-90 FundA Microsoft        5000
Jun-90 FundA Apple            6000
Jun-90 FundA Microsoft        4000
Jun-90 FundA Oracle           2000
Oct-90 FundA Apple            6000
Oct-90 FundA Oracle           2000
Oct-90 FundA Facebook         2000
run;

proc sql;
  create table CARTESIAN as 
  select * from (select unique DATE  from HAVE)
               ,(select unique FUND  from HAVE)
               ,(select unique STOCK from HAVE)
  order by FUND, STOCK, DATE;
quit;

data CHANGES(index=(A=(FUND DATE STOCK))); 
  merge HAVE CARTESIAN;
  by FUND STOCK DATE;
  if SHARES_HELD=. then SHARES_HELD=0;
  CHANGE=ifn(first.STOCK,SHARES_HELD,SHARES_HELD-lag(SHARES_HELD));
run;
      
data WANT; 
  set CHANGES;
  by FUND DATE STOCK;
run;  

proc print noobs;
  var DATE STOCK CHANGE;
  where CHANGE;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellpadding="5" cellspacing="0"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="l header" scope="col"&gt;DATE&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;STOCK&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;CHANGE&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;Apr-90&lt;/TD&gt;
&lt;TD class="l data"&gt;Apple&lt;/TD&gt;
&lt;TD class="r data"&gt;5000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;Apr-90&lt;/TD&gt;
&lt;TD class="l data"&gt;Microsoft&lt;/TD&gt;
&lt;TD class="r data"&gt;5000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;Jun-90&lt;/TD&gt;
&lt;TD class="l data"&gt;Apple&lt;/TD&gt;
&lt;TD class="r data"&gt;1000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;Jun-90&lt;/TD&gt;
&lt;TD class="l data"&gt;Microsoft&lt;/TD&gt;
&lt;TD class="r data"&gt;-1000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;Jun-90&lt;/TD&gt;
&lt;TD class="l data"&gt;Oracle&lt;/TD&gt;
&lt;TD class="r data"&gt;2000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;Oct-90&lt;/TD&gt;
&lt;TD class="l data"&gt;Facebook&lt;/TD&gt;
&lt;TD class="r data"&gt;2000&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;Oct-90&lt;/TD&gt;
&lt;TD class="l data"&gt;Microsoft&lt;/TD&gt;
&lt;TD class="r data"&gt;-4000&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Thu, 01 Jun 2017 03:31:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/computing-buys-and-sells-in-a-portfolio/m-p/363299#M274877</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-06-01T03:31:33Z</dc:date>
    </item>
    <item>
      <title>Re: computing buys and sells in a portfolio</title>
      <link>https://communities.sas.com/t5/SAS-Programming/computing-buys-and-sells-in-a-portfolio/m-p/363301#M274878</link>
      <description>&lt;P&gt;Slightly more compact:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data CHANGES(index=(A=(FUND DATE STOCK))); 
  merge HAVE CARTESIAN;
  by FUND STOCK DATE;
  CHANGE=sum(0,SHARES_HELD,-lag(SHARES_HELD)*^first.STOCK);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 03:31:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/computing-buys-and-sells-in-a-portfolio/m-p/363301#M274878</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2017-06-01T03:31:51Z</dc:date>
    </item>
    <item>
      <title>Re: computing buys and sells in a portfolio</title>
      <link>https://communities.sas.com/t5/SAS-Programming/computing-buys-and-sells-in-a-portfolio/m-p/363302#M274879</link>
      <description>&lt;PRE&gt;data want (drop=_:);
  set have;
  retain _date _fund;
  if not missing(date) then _date=date;
  else date=_date;
  if not missing(fund) then _fund=fund;
  else fund=_fund;
run;

proc sort data=want;
  by stock date;
run;

data want (drop=_:);
  set want;
  by stock;
  _shares_held=ifn(first.stock,0,lag(Shares_held));
  change=Shares_held-_Shares_held;
run;

proc sort data=want;
  by date fund stock;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 03:32:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/computing-buys-and-sells-in-a-portfolio/m-p/363302#M274879</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-06-01T03:32:55Z</dc:date>
    </item>
    <item>
      <title>Re: computing buys and sells in a portfolio</title>
      <link>https://communities.sas.com/t5/SAS-Programming/computing-buys-and-sells-in-a-portfolio/m-p/363305#M274880</link>
      <description>&lt;P&gt;Thank art297 and ChrisNZ, both of your solutions work..&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 03:56:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/computing-buys-and-sells-in-a-portfolio/m-p/363305#M274880</guid>
      <dc:creator>Ramani</dc:creator>
      <dc:date>2017-06-01T03:56:06Z</dc:date>
    </item>
    <item>
      <title>Re: computing buys and sells in a portfolio</title>
      <link>https://communities.sas.com/t5/SAS-Programming/computing-buys-and-sells-in-a-portfolio/m-p/363510#M274881</link>
      <description>Art297, This solution does not work when I extend the data.&lt;BR /&gt;1990-04-30 FundA Apple 5000&lt;BR /&gt;1990-04-30 FundA Microsoft 5000&lt;BR /&gt;1990-06-30 FundA Apple 6000&lt;BR /&gt;1990-06-30 FundA Microsoft 4000&lt;BR /&gt;1990-06-30 FundA Oracle 2000&lt;BR /&gt;1990-09-30 FundA Apple 6000&lt;BR /&gt;1990-09-30 FundA Oracle 2000&lt;BR /&gt;1990-09-30 FundA Facebook 2000&lt;BR /&gt;1990-12-30 FundA Microsoft 1000&lt;BR /&gt;1990-12-30 FundA Apple 6000</description>
      <pubDate>Thu, 01 Jun 2017 16:42:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/computing-buys-and-sells-in-a-portfolio/m-p/363510#M274881</guid>
      <dc:creator>Ramani</dc:creator>
      <dc:date>2017-06-01T16:42:02Z</dc:date>
    </item>
    <item>
      <title>Re: computing buys and sells in a portfolio</title>
      <link>https://communities.sas.com/t5/SAS-Programming/computing-buys-and-sells-in-a-portfolio/m-p/363514#M274882</link>
      <description>&lt;P&gt;What doesn't work? You'll have to be more specific. Your data format and values have changed quite a bit from your original question, thus I removed the data clean-up section of my code. Unless I'm missing something, it appears to follow your specs:&lt;/P&gt;
&lt;PRE&gt;data have;
  informat date yymmdd10.;
  informat fund $8.;
  informat stock $9.;
  input Date    Fund &amp;amp;      Stock          Shares_held;
  cards;
1990-04-30  FundA  Apple  5000
1990-04-30  FundA  Microsoft  5000
1990-06-30  FundA  Apple  6000
1990-06-30  FundA  Microsoft  4000
1990-06-30  FundA  Oracle  2000
1990-09-30  FundA  Apple  6000
1990-09-30  FundA  Oracle  2000
1990-09-30  FundA  Facebook  2000
1990-12-30  FundA  Microsoft  1000
1990-12-30  FundA  Apple  6000
;

proc sort data=have out=want;
  by stock date;
run;

data want (drop=_:);
  set want;
  by stock;
  _shares_held=ifn(first.stock,0,lag(Shares_held));
  change=Shares_held-_Shares_held;
run;

proc sort data=want;
  by date fund stock;
run;&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Thu, 01 Jun 2017 16:56:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/computing-buys-and-sells-in-a-portfolio/m-p/363514#M274882</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-06-01T16:56:29Z</dc:date>
    </item>
    <item>
      <title>Re: computing buys and sells in a portfolio</title>
      <link>https://communities.sas.com/t5/SAS-Programming/computing-buys-and-sells-in-a-portfolio/m-p/363900#M274883</link>
      <description>&lt;P&gt;I apologise for not being specific earlier,&lt;/P&gt;&lt;P&gt;Your code captures the difference in shares from one quarter to another. but it does not capture when all the shares of a stock have been sold.&lt;BR /&gt;E.g. All of Microsoft is sold in September and hence I need a value where shares_change = -4000 or microsoft.&lt;BR /&gt;Microsoft is added back to the portfolio in December, but the value wrongly shows as -3000.&lt;BR /&gt;Oracle is also sold in December and hence it is not being captured in December.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Jun 2017 18:09:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/computing-buys-and-sells-in-a-portfolio/m-p/363900#M274883</guid>
      <dc:creator>Ramani</dc:creator>
      <dc:date>2017-06-02T18:09:47Z</dc:date>
    </item>
    <item>
      <title>Re: computing buys and sells in a portfolio</title>
      <link>https://communities.sas.com/t5/SAS-Programming/computing-buys-and-sells-in-a-portfolio/m-p/363906#M274884</link>
      <description>&lt;P&gt;Got it! I didn't understand that missing entries meant that all shares had been sold. As such, yes, I'd stick with the code suggested by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&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>Fri, 02 Jun 2017 18:40:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/computing-buys-and-sells-in-a-portfolio/m-p/363906#M274884</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-06-02T18:40:16Z</dc:date>
    </item>
  </channel>
</rss>

