<?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 Calculating monthly total returns for each investor in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Calculating-monthly-total-returns-for-each-investor/m-p/75573#M21926</link>
    <description>I have to calculate monthly total returns for each investor (ID) like&lt;BR /&gt;
&lt;BR /&gt;
ID	CYMD	RMTOT&lt;BR /&gt;
1	200201	0.06&lt;BR /&gt;
1	200202	-0.04&lt;BR /&gt;
1	200203	-0.03&lt;BR /&gt;
1	200204	0.03&lt;BR /&gt;
etc.&lt;BR /&gt;
&lt;BR /&gt;
Here is an example of the data. IC represents assets, CYMD (year, month) and R monthly return of an asset.&lt;BR /&gt;
&lt;BR /&gt;
ID	IC	CYMD	R&lt;BR /&gt;
1	1	200201	0.03&lt;BR /&gt;
1	2	200201	0.02&lt;BR /&gt;
1	3	200201	0.01&lt;BR /&gt;
1	1	200202	0.01&lt;BR /&gt;
1	2	200202	-0.05&lt;BR /&gt;
1	1	200203	-0.05&lt;BR /&gt;
1	2	200203	0.02&lt;BR /&gt;
1	1	200204	0.02&lt;BR /&gt;
1	4	200204	0.01&lt;BR /&gt;
2	1	200201	0.03&lt;BR /&gt;
2	2	200201	0.02&lt;BR /&gt;
2	3	200201	0.01&lt;BR /&gt;
2	1	200202	0.01&lt;BR /&gt;
2	2	200202	-0.05&lt;BR /&gt;
2	3	200202	-0.05&lt;BR /&gt;
&lt;BR /&gt;
As you see assets and investing time may variate over time.&lt;BR /&gt;
&lt;BR /&gt;
I tried to do it by calculating a new variable RPMTOT. Obviously when reading the data and setting a new variable you may not use the lag-function.&lt;BR /&gt;
&lt;BR /&gt;
data bcd ;&lt;BR /&gt;
	set abc ;&lt;BR /&gt;
	if CYMD ^= lag( CYMD ) then RMTOT = R ;&lt;BR /&gt;
	else RMTOT = lag( RMTOT ) + R ;&lt;BR /&gt;
	keep ID IC CYMD R RMTOT ;&lt;BR /&gt;
	&lt;BR /&gt;
Now I would pick up the last RMTOT value for each month and for each investor to get the result I want.&lt;BR /&gt;
	&lt;BR /&gt;
I think there has to be easier and working way to do it but how it should be done?</description>
    <pubDate>Thu, 26 Feb 2009 08:18:45 GMT</pubDate>
    <dc:creator>deleted_user</dc:creator>
    <dc:date>2009-02-26T08:18:45Z</dc:date>
    <item>
      <title>Calculating monthly total returns for each investor</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculating-monthly-total-returns-for-each-investor/m-p/75573#M21926</link>
      <description>I have to calculate monthly total returns for each investor (ID) like&lt;BR /&gt;
&lt;BR /&gt;
ID	CYMD	RMTOT&lt;BR /&gt;
1	200201	0.06&lt;BR /&gt;
1	200202	-0.04&lt;BR /&gt;
1	200203	-0.03&lt;BR /&gt;
1	200204	0.03&lt;BR /&gt;
etc.&lt;BR /&gt;
&lt;BR /&gt;
Here is an example of the data. IC represents assets, CYMD (year, month) and R monthly return of an asset.&lt;BR /&gt;
&lt;BR /&gt;
ID	IC	CYMD	R&lt;BR /&gt;
1	1	200201	0.03&lt;BR /&gt;
1	2	200201	0.02&lt;BR /&gt;
1	3	200201	0.01&lt;BR /&gt;
1	1	200202	0.01&lt;BR /&gt;
1	2	200202	-0.05&lt;BR /&gt;
1	1	200203	-0.05&lt;BR /&gt;
1	2	200203	0.02&lt;BR /&gt;
1	1	200204	0.02&lt;BR /&gt;
1	4	200204	0.01&lt;BR /&gt;
2	1	200201	0.03&lt;BR /&gt;
2	2	200201	0.02&lt;BR /&gt;
2	3	200201	0.01&lt;BR /&gt;
2	1	200202	0.01&lt;BR /&gt;
2	2	200202	-0.05&lt;BR /&gt;
2	3	200202	-0.05&lt;BR /&gt;
&lt;BR /&gt;
As you see assets and investing time may variate over time.&lt;BR /&gt;
&lt;BR /&gt;
I tried to do it by calculating a new variable RPMTOT. Obviously when reading the data and setting a new variable you may not use the lag-function.&lt;BR /&gt;
&lt;BR /&gt;
data bcd ;&lt;BR /&gt;
	set abc ;&lt;BR /&gt;
	if CYMD ^= lag( CYMD ) then RMTOT = R ;&lt;BR /&gt;
	else RMTOT = lag( RMTOT ) + R ;&lt;BR /&gt;
	keep ID IC CYMD R RMTOT ;&lt;BR /&gt;
	&lt;BR /&gt;
Now I would pick up the last RMTOT value for each month and for each investor to get the result I want.&lt;BR /&gt;
	&lt;BR /&gt;
I think there has to be easier and working way to do it but how it should be done?</description>
      <pubDate>Thu, 26 Feb 2009 08:18:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculating-monthly-total-returns-for-each-investor/m-p/75573#M21926</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-02-26T08:18:45Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating monthly total returns for each investor</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculating-monthly-total-returns-for-each-investor/m-p/75574#M21927</link>
      <description>I already found an answer to my problem...&lt;BR /&gt;
&lt;BR /&gt;
I found it from the thread "Proc means summary of "duplicate" values" &lt;BR /&gt;
&lt;A href="http://support.sas.com/forums/thread.jspa?threadID=4874&amp;amp;tstart=15" target="_blank"&gt;http://support.sas.com/forums/thread.jspa?threadID=4874&amp;amp;tstart=15&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
proc means nway data = abc missing noprint;&lt;BR /&gt;
	var R ;&lt;BR /&gt;
	class ID CYMD ;&lt;BR /&gt;
	output out = bcd ( drop= _TYPE_ _FREQ_ ) &lt;BR /&gt;
	sum = RMTOT;&lt;BR /&gt;
run ;&lt;BR /&gt;
&lt;BR /&gt;
Thank you guys!</description>
      <pubDate>Thu, 26 Feb 2009 08:39:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculating-monthly-total-returns-for-each-investor/m-p/75574#M21927</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-02-26T08:39:57Z</dc:date>
    </item>
  </channel>
</rss>

