<?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: Addition of Values for Current and Previous Values. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Addition-of-Values-for-Current-and-Previous-Values/m-p/50814#M10636</link>
    <description>Hi KSharp. Thank you very much. This is the answer that I've been looking for so long. Yet you did it in a simple way and yet easy to understand. &lt;BR /&gt;
&lt;BR /&gt;
Really appreciate what you have been doing to help those who dabble in SAS programming, including me.</description>
    <pubDate>Thu, 21 Apr 2011 01:47:56 GMT</pubDate>
    <dc:creator>KYW</dc:creator>
    <dc:date>2011-04-21T01:47:56Z</dc:date>
    <item>
      <title>Addition of Values for Current and Previous Values.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Addition-of-Values-for-Current-and-Previous-Values/m-p/50804#M10626</link>
      <description>Hi all. I have a complicated problem regarding the sums of relevant values in different periods that needs to be solved but not sure how.&lt;BR /&gt;
&lt;BR /&gt;
The simplified situation is like this:-&lt;BR /&gt;
I have a table A with following fields:&lt;BR /&gt;
Year, Month, Stock, Price.&lt;BR /&gt;
Let's say from table A I have data from Year 2008 to 2011 with each year having 100 of different stocks with different prices each month. &lt;BR /&gt;
&lt;BR /&gt;
With above data fields, I need to create a dataset B with the following fields:&lt;BR /&gt;
Year, Month, Stock, Current_MTD_Price_Sum,  Previous_MTD_Price_Sum, Current_YTD_Price_Sum, Previous_YTD_Price_Sum.&lt;BR /&gt;
&lt;BR /&gt;
The Current_MTD_Price_Sum is the sum of prices of each stock from that month itself while Previous_MTD_Price_Sum refers to the price sum of the same stock in a year before. &lt;BR /&gt;
Eg. If the year is 2009 and the month is April, the Current_MTD_Price_Sum would refer to the sum of prices of that stock Z in April 2009 while Previous_MTD_Price_Sum would show the sum of prices for April 2008 for the same stock Z.&lt;BR /&gt;
&lt;BR /&gt;
Meanwhile, Current_YTD_Price_Sum would show the total sum for the stock from January to April 2010 if the current month is April and Current Year is 2010. Previous_YTD_Price_Sum would be the sum of prices for the stock from January to April 2009.&lt;BR /&gt;
&lt;BR /&gt;
I have tried to use retain and sum statements but they can only add to a certain period. The tables may look simple but the process to get the correct values has been a great challenge for me so far. &lt;BR /&gt;
&lt;BR /&gt;
Thanks for your time.&lt;BR /&gt;
&lt;BR /&gt;
KYW</description>
      <pubDate>Thu, 14 Apr 2011 18:19:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Addition-of-Values-for-Current-and-Previous-Values/m-p/50804#M10626</guid>
      <dc:creator>KYW</dc:creator>
      <dc:date>2011-04-14T18:19:33Z</dc:date>
    </item>
    <item>
      <title>Re: Addition of Values for Current and Previous Values.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Addition-of-Values-for-Current-and-Previous-Values/m-p/50805#M10627</link>
      <description>Maybe not trying to generate B in one single data step make the task much easier.&lt;BR /&gt;
Similar like this, but need some polishing:&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
create table B1 as&lt;BR /&gt;
select stock, month, year, sum(price) as Current_MTD_Price_Sum&lt;BR /&gt;
from A&lt;BR /&gt;
group by stock, month, year;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
proc sort data=b1;&lt;BR /&gt;
by stock year month;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data B;&lt;BR /&gt;
set B1;&lt;BR /&gt;
retain Previous_MTD_Price_Sum &lt;BR /&gt;
    Previous_YTD_Price_Sum&lt;BR /&gt;
    Current_YTD_Price_Sum&lt;BR /&gt;
;&lt;BR /&gt;
by stock year month;&lt;BR /&gt;
if first.stock then Previous_YTD_Price_Sum=0;&lt;BR /&gt;
if first.year then do;&lt;BR /&gt;
    Current_YTD_Price_Sum=Current_MTD_Price_Sum;&lt;BR /&gt;
end;&lt;BR /&gt;
Current_YTD_Price_Sum+Current_MTD_Price_Sum;&lt;BR /&gt;
output;&lt;BR /&gt;
Previous_MTD_Price_Sum=Current_MTD_Price_Sum;&lt;BR /&gt;
if last.year then &lt;BR /&gt;
Previous_YTD_Price_Sum=Current_YTD_Price_Sum;&lt;BR /&gt;
run;</description>
      <pubDate>Thu, 14 Apr 2011 19:08:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Addition-of-Values-for-Current-and-Previous-Values/m-p/50805#M10627</guid>
      <dc:creator>ASASProgrammer</dc:creator>
      <dc:date>2011-04-14T19:08:33Z</dc:date>
    </item>
    <item>
      <title>Re: Addition of Values for Current and Previous Values.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Addition-of-Values-for-Current-and-Previous-Values/m-p/50806#M10628</link>
      <description>Can you plz post some dummy data into the forum?&lt;BR /&gt;
It spend me lots of time to input data.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data temp;&lt;BR /&gt;
input year  stock $ month  price ;&lt;BR /&gt;
cards;&lt;BR /&gt;
2004 CVX0504 1 18.2&lt;BR /&gt;
2004 CVX0504 1 19.61&lt;BR /&gt;
2004 CVX0504 2 18.38&lt;BR /&gt;
2004 CVX0504 2 19.58&lt;BR /&gt;
2004 CVX0504 3 19.61&lt;BR /&gt;
2004 CVX0504 4 18.38&lt;BR /&gt;
2004 CVX0504 5 19.58&lt;BR /&gt;
2004 CVX0504 6 19.58&lt;BR /&gt;
2004 CVX0504 7 19.58&lt;BR /&gt;
2004 CVX0504 8 19.58&lt;BR /&gt;
2004 CVX0504 9 19.58&lt;BR /&gt;
2004 CVX0504 10 19.58&lt;BR /&gt;
2004 CVX0504 11 19.58&lt;BR /&gt;
2004 CVX0504 12 19.58&lt;BR /&gt;
2005 CVX0504 1 18.55&lt;BR /&gt;
2005 CVX0504 1 19.67&lt;BR /&gt;
2005 CVX0504 2 19.37&lt;BR /&gt;
2005 CVX0504 2 20.21&lt;BR /&gt;
2005 CVX0504 3 19.61&lt;BR /&gt;
2005 CVX0504 4 18.38&lt;BR /&gt;
2005 CVX0504 5 19.58&lt;BR /&gt;
2005 CVX0504 6 19.58&lt;BR /&gt;
2005 CVX0504 7 19.58&lt;BR /&gt;
2005 CVX0504 8 19.58&lt;BR /&gt;
2005 CVX0504 9 19.58&lt;BR /&gt;
2005 CVX0504 10 19.58&lt;BR /&gt;
2005 CVX0504 11 19.58&lt;BR /&gt;
2005 CVX0504 12 19.58&lt;BR /&gt;
2006 CVX0604 1 19.04&lt;BR /&gt;
2006 CVX0604 1 17.5&lt;BR /&gt;
2006 CVX0604 2 18&lt;BR /&gt;
2006 CVX0604 2 17.61&lt;BR /&gt;
2006 CVX0604 3 19.61&lt;BR /&gt;
2006 CVX0604 4 18.38&lt;BR /&gt;
2006 CVX0604 5 19.58&lt;BR /&gt;
2006 CVX0604 6 19.58&lt;BR /&gt;
2006 CVX0604 7 19.58&lt;BR /&gt;
2006 CVX0604 8 19.58&lt;BR /&gt;
2006 CVX0604 9 19.58&lt;BR /&gt;
2006 CVX0604 10 19.58&lt;BR /&gt;
2006 CVX0604 11 19.58&lt;BR /&gt;
2006 CVX0604 12 19.58&lt;BR /&gt;
2007 CVX0604 1 18.09&lt;BR /&gt;
2007 CVX0604 1 17.35&lt;BR /&gt;
2007 CVX0604 2 17.9&lt;BR /&gt;
2007 CVX0604 2 17.9&lt;BR /&gt;
2007 CVX0604 3 19.61&lt;BR /&gt;
2007 CVX0604 4 18.38&lt;BR /&gt;
2007 CVX0604 5 19.58&lt;BR /&gt;
2007 CVX0604 6 19.58&lt;BR /&gt;
2007 CVX0604 7 19.58&lt;BR /&gt;
2007 CVX0604 8 19.58&lt;BR /&gt;
2007 CVX0604 9 19.58&lt;BR /&gt;
2007 CVX0604 10 19.58&lt;BR /&gt;
2007 CVX0604 11 19.58&lt;BR /&gt;
2007 CVX0604 12 19.58&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
proc sort data=temp;&lt;BR /&gt;
 by stock year month;&lt;BR /&gt;
run;&lt;BR /&gt;
data want;&lt;BR /&gt;
 set temp;&lt;BR /&gt;
 by stock year month ;&lt;BR /&gt;
 if first.month then current_mtd_price_sum=0;&lt;BR /&gt;
 if first.year then current_ytd_price_sum=0;&lt;BR /&gt;
 current_mtd_price_sum+price;&lt;BR /&gt;
 current_ytd_price_sum+price;&lt;BR /&gt;
 if last.month then output;&lt;BR /&gt;
run;&lt;BR /&gt;
data want;&lt;BR /&gt;
 set want;&lt;BR /&gt;
 previous_mtd_price_sum=lag12(current_mtd_price_sum);&lt;BR /&gt;
 previous_ytd_price_sum=lag12(current_ytd_price_sum);&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Fri, 15 Apr 2011 04:52:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Addition-of-Values-for-Current-and-Previous-Values/m-p/50806#M10628</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-04-15T04:52:32Z</dc:date>
    </item>
    <item>
      <title>Re: Addition of Values for Current and Previous Values.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Addition-of-Values-for-Current-and-Previous-Values/m-p/50807#M10629</link>
      <description>Hi ShareMyAnswer. Thanks for your suggestion. Perhaps breaking down the problem into parts will make it easier to find the solution. &lt;BR /&gt;
&lt;BR /&gt;
Will give it a try.</description>
      <pubDate>Sat, 16 Apr 2011 02:07:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Addition-of-Values-for-Current-and-Previous-Values/m-p/50807#M10629</guid>
      <dc:creator>KYW</dc:creator>
      <dc:date>2011-04-16T02:07:37Z</dc:date>
    </item>
    <item>
      <title>Re: Addition of Values for Current and Previous Values.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Addition-of-Values-for-Current-and-Previous-Values/m-p/50808#M10630</link>
      <description>Thanks again KSharp and your efforts in creating dummy data. Your guidance is very helpful.&lt;BR /&gt;
One question I have is that if lag12() is used to get the values 1 year back, what if I need to further split the month into weeks? We know that 1 month has around 4 or 5 weeks. Can lag12( ) still be used in this situation or do we have to another solution as I don't think lag4 will solve the problem.&lt;BR /&gt;
&lt;BR /&gt;
Thank you.</description>
      <pubDate>Sat, 16 Apr 2011 02:14:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Addition-of-Values-for-Current-and-Previous-Values/m-p/50808#M10630</guid>
      <dc:creator>KYW</dc:creator>
      <dc:date>2011-04-16T02:14:37Z</dc:date>
    </item>
    <item>
      <title>Re: Addition of Values for Current and Previous Values.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Addition-of-Values-for-Current-and-Previous-Values/m-p/50809#M10631</link>
      <description>Yes.You are right.For this situation, the format about week such as weekdate. will help you some.Give some dummy data,I will try it.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Mon, 18 Apr 2011 05:21:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Addition-of-Values-for-Current-and-Previous-Values/m-p/50809#M10631</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-04-18T05:21:54Z</dc:date>
    </item>
    <item>
      <title>Re: Addition of Values for Current and Previous Values.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Addition-of-Values-for-Current-and-Previous-Values/m-p/50810#M10632</link>
      <description>Okay. Let's say below is the dummy data with the following fields:&lt;BR /&gt;
Year, Month, Week, Stock, Price.&lt;BR /&gt;
I will just take 1 stock for easy understanding. We know that 1 year has 52 weeks. So the weeks are counted as a continuation from week 1 until week 52 (1 year) corresponding to the relevant months. So now that instead of just summing up the MTD and YTD prices for the stock, we need to split it to the week.&lt;BR /&gt;
&lt;BR /&gt;
Eg. Previous_WTD_Price_SUM for Week 18 (Which falls on April) in Year 2005 is equivalent to the Current_WTD_Price_Sum for Week 18 in Year 2004 while maintaining what we have already done in Current and Previous MTD and YTD Price Values. &lt;BR /&gt;
&lt;BR /&gt;
What makes thing more complicated is that the Year, Month and Week are all characters with a '0' to make it looks double digits.&lt;BR /&gt;
&lt;BR /&gt;
2004  01  01  A  21&lt;BR /&gt;
2004  01  02  A  25&lt;BR /&gt;
2004  01  03  A  22&lt;BR /&gt;
2004  01  04  A  23&lt;BR /&gt;
2004  01  05  A  24&lt;BR /&gt;
2004  02  06  A  27&lt;BR /&gt;
2004  02  07  A  25&lt;BR /&gt;
2004  02  08  A  23&lt;BR /&gt;
2004  02  09  A  23&lt;BR /&gt;
2004  03  10  A  22&lt;BR /&gt;
2004  03  11  A  22&lt;BR /&gt;
2004  03  12  A  22&lt;BR /&gt;
2004  03  13  A  26&lt;BR /&gt;
2004  03  14  A  24&lt;BR /&gt;
2004  04  15  A  23&lt;BR /&gt;
2004  04  16  A  25&lt;BR /&gt;
2004  04  17  A  22&lt;BR /&gt;
2004  04  18  A  25&lt;BR /&gt;
2004  05  19  A  22&lt;BR /&gt;
2004  05  20  A  25&lt;BR /&gt;
2004  05  21  A  21&lt;BR /&gt;
2004  05  22  A  23&lt;BR /&gt;
2004  05  23  A  21&lt;BR /&gt;
2004  06  24  A  21&lt;BR /&gt;
2004  06  25  A  22&lt;BR /&gt;
2004  06  26  A  22&lt;BR /&gt;
2004  06  27  A  23&lt;BR /&gt;
2004  07  28  A  21&lt;BR /&gt;
2004  07  29  A  24&lt;BR /&gt;
2004  07  30  A  26&lt;BR /&gt;
2004  07  31  A  27&lt;BR /&gt;
2004  07  32  A  28&lt;BR /&gt;
2004  08  33  A  27&lt;BR /&gt;
2004  08  34  A  27&lt;BR /&gt;
2004  08  35  A  26&lt;BR /&gt;
2004  08  36  A  23&lt;BR /&gt;
2004  08  37  A  24&lt;BR /&gt;
2004  09  38  A  21&lt;BR /&gt;
2004  09  39  A  25&lt;BR /&gt;
2004  09  40  A  22&lt;BR /&gt;
2004  09  41  A  23&lt;BR /&gt;
2004  10  42  A  22&lt;BR /&gt;
2004  10  43  A  25&lt;BR /&gt;
2004  10  44  A  27&lt;BR /&gt;
2004  10  45  A  24&lt;BR /&gt;
2004  11  46  A  23&lt;BR /&gt;
2004  11  47  A  21&lt;BR /&gt;
2004  11  48  A  22&lt;BR /&gt;
2004  11  49  A  20&lt;BR /&gt;
2004  12  50  A  21&lt;BR /&gt;
2004  12  51  A  25&lt;BR /&gt;
2004  12  52  A  22&lt;BR /&gt;
================================&lt;BR /&gt;
2005  01  01  A  21&lt;BR /&gt;
2005  01  02  A  25&lt;BR /&gt;
2005  01  03  A  22&lt;BR /&gt;
2005  01  04  A  23&lt;BR /&gt;
2005  01  05  A  24&lt;BR /&gt;
2005  02  06  A  27&lt;BR /&gt;
2005  02  07  A  25&lt;BR /&gt;
2005  02  08  A  23&lt;BR /&gt;
2005  02  09  A  23&lt;BR /&gt;
2005  03  10  A  22&lt;BR /&gt;
2005  03  11  A  22&lt;BR /&gt;
2005  03  12  A  22&lt;BR /&gt;
2005  03  13  A  26&lt;BR /&gt;
2005  03  14  A  24&lt;BR /&gt;
2005  04  15  A  23&lt;BR /&gt;
2005  04  16  A  25&lt;BR /&gt;
2005  04  17  A  22&lt;BR /&gt;
2005  04  18  A  25&lt;BR /&gt;
2005  05  19  A  22&lt;BR /&gt;
2005  05  20  A  25&lt;BR /&gt;
2005  05  21  A  21&lt;BR /&gt;
2005  05  22  A  23&lt;BR /&gt;
2005  05  23  A  21&lt;BR /&gt;
2005  06  24  A  21&lt;BR /&gt;
2005  06  25  A  22&lt;BR /&gt;
2005  06  26  A  22&lt;BR /&gt;
2005  06  27  A  23&lt;BR /&gt;
2005  07  28  A  21&lt;BR /&gt;
2005  07  29  A  24&lt;BR /&gt;
2005  07  30  A  26&lt;BR /&gt;
2005  07  31  A  27&lt;BR /&gt;
2005  07  32  A  28&lt;BR /&gt;
2005  08  33  A  27&lt;BR /&gt;
2005  08  34  A  27&lt;BR /&gt;
2005  08  35  A  26&lt;BR /&gt;
2005  08  36  A  23&lt;BR /&gt;
2005  08  37  A  24&lt;BR /&gt;
2005  09  38  A  21&lt;BR /&gt;
2005  09  39  A  25&lt;BR /&gt;
2005  09  40  A  22&lt;BR /&gt;
2005  09  41  A  23&lt;BR /&gt;
2005  10  42  A  22&lt;BR /&gt;
2005  10  43  A  25&lt;BR /&gt;
2005  10  44  A  27&lt;BR /&gt;
2005  10  45  A  24&lt;BR /&gt;
2005  11  46  A  23&lt;BR /&gt;
2005  11  47  A  21&lt;BR /&gt;
2005  11  48  A  22&lt;BR /&gt;
2005  11  49  A  20&lt;BR /&gt;
2005  12  50  A  21&lt;BR /&gt;
2005  12  51  A  25&lt;BR /&gt;
2005  12  52  A  22</description>
      <pubDate>Mon, 18 Apr 2011 10:55:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Addition-of-Values-for-Current-and-Previous-Values/m-p/50810#M10632</guid>
      <dc:creator>KYW</dc:creator>
      <dc:date>2011-04-18T10:55:30Z</dc:date>
    </item>
    <item>
      <title>Re: Addition of Values for Current and Previous Values.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Addition-of-Values-for-Current-and-Previous-Values/m-p/50811#M10633</link>
      <description>OK.It looks like merge several tables.&lt;BR /&gt;
Since you do not post the output You want.So check it to see whether it is what you need.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data temp;&lt;BR /&gt;
input _year $  _month $ _week $ stock $ price ;&lt;BR /&gt;
cards;&lt;BR /&gt;
2004 01 01 A 21&lt;BR /&gt;
2004 01 02 A 25&lt;BR /&gt;
2004 01 03 A 22&lt;BR /&gt;
2004 01 04 A 23&lt;BR /&gt;
2004 01 05 A 24&lt;BR /&gt;
2004 02 06 A 27&lt;BR /&gt;
2004 02 07 A 25&lt;BR /&gt;
2004 02 08 A 23&lt;BR /&gt;
2004 02 09 A 23&lt;BR /&gt;
2004 03 10 A 22&lt;BR /&gt;
2004 03 11 A 22&lt;BR /&gt;
2004 03 12 A 22&lt;BR /&gt;
2004 03 13 A 26&lt;BR /&gt;
2004 03 14 A 24&lt;BR /&gt;
2004 04 15 A 23&lt;BR /&gt;
2004 04 16 A 25&lt;BR /&gt;
2004 04 17 A 22&lt;BR /&gt;
2004 04 18 A 25&lt;BR /&gt;
2004 05 19 A 22&lt;BR /&gt;
2004 05 20 A 25&lt;BR /&gt;
2004 05 21 A 21&lt;BR /&gt;
2004 05 22 A 23&lt;BR /&gt;
2004 05 23 A 21&lt;BR /&gt;
2004 06 24 A 21&lt;BR /&gt;
2004 06 25 A 22&lt;BR /&gt;
2004 06 26 A 22&lt;BR /&gt;
2004 06 27 A 23&lt;BR /&gt;
2004 07 28 A 21&lt;BR /&gt;
2004 07 29 A 24&lt;BR /&gt;
2004 07 30 A 26&lt;BR /&gt;
2004 07 31 A 27&lt;BR /&gt;
2004 07 32 A 28&lt;BR /&gt;
2004 08 33 A 27&lt;BR /&gt;
2004 08 34 A 27&lt;BR /&gt;
2004 08 35 A 26&lt;BR /&gt;
2004 08 36 A 23&lt;BR /&gt;
2004 08 37 A 24&lt;BR /&gt;
2004 09 38 A 21&lt;BR /&gt;
2004 09 39 A 25&lt;BR /&gt;
2004 09 40 A 22&lt;BR /&gt;
2004 09 41 A 23&lt;BR /&gt;
2004 10 42 A 22&lt;BR /&gt;
2004 10 43 A 25&lt;BR /&gt;
2004 10 44 A 27&lt;BR /&gt;
2004 10 45 A 24&lt;BR /&gt;
2004 11 46 A 23&lt;BR /&gt;
2004 11 47 A 21&lt;BR /&gt;
2004 11 48 A 22&lt;BR /&gt;
2004 11 49 A 20&lt;BR /&gt;
2004 12 50 A 21&lt;BR /&gt;
2004 12 51 A 25&lt;BR /&gt;
2004 12 52 A 22&lt;BR /&gt;
2005 01 01 A 21&lt;BR /&gt;
2005 01 02 A 25&lt;BR /&gt;
2005 01 03 A 22&lt;BR /&gt;
2005 01 04 A 23&lt;BR /&gt;
2005 01 05 A 24&lt;BR /&gt;
2005 02 06 A 27&lt;BR /&gt;
2005 02 07 A 25&lt;BR /&gt;
2005 02 08 A 23&lt;BR /&gt;
2005 02 09 A 23&lt;BR /&gt;
2005 03 10 A 22&lt;BR /&gt;
2005 03 11 A 22&lt;BR /&gt;
2005 03 12 A 22&lt;BR /&gt;
2005 03 13 A 26&lt;BR /&gt;
2005 03 14 A 24&lt;BR /&gt;
2005 04 15 A 23&lt;BR /&gt;
2005 04 16 A 25&lt;BR /&gt;
2005 04 17 A 22&lt;BR /&gt;
2005 04 18 A 25&lt;BR /&gt;
2005 05 19 A 22&lt;BR /&gt;
2005 05 20 A 25&lt;BR /&gt;
2005 05 21 A 21&lt;BR /&gt;
2005 05 22 A 23&lt;BR /&gt;
2005 05 23 A 21&lt;BR /&gt;
2005 06 24 A 21&lt;BR /&gt;
2005 06 25 A 22&lt;BR /&gt;
2005 06 26 A 22&lt;BR /&gt;
2005 06 27 A 23&lt;BR /&gt;
2005 07 28 A 21&lt;BR /&gt;
2005 07 29 A 24&lt;BR /&gt;
2005 07 30 A 26&lt;BR /&gt;
2005 07 31 A 27&lt;BR /&gt;
2005 07 32 A 28&lt;BR /&gt;
2005 08 33 A 27&lt;BR /&gt;
2005 08 34 A 27&lt;BR /&gt;
2005 08 35 A 26&lt;BR /&gt;
2005 08 36 A 23&lt;BR /&gt;
2005 08 37 A 24&lt;BR /&gt;
2005 09 38 A 21&lt;BR /&gt;
2005 09 39 A 25&lt;BR /&gt;
2005 09 40 A 22&lt;BR /&gt;
2005 09 41 A 23&lt;BR /&gt;
2005 10 42 A 22&lt;BR /&gt;
2005 10 43 A 25&lt;BR /&gt;
2005 10 44 A 27&lt;BR /&gt;
2005 10 45 A 24&lt;BR /&gt;
2005 11 46 A 23&lt;BR /&gt;
2005 11 47 A 21&lt;BR /&gt;
2005 11 48 A 22&lt;BR /&gt;
2005 11 49 A 20&lt;BR /&gt;
2005 12 50 A 21&lt;BR /&gt;
2005 12 51 A 25&lt;BR /&gt;
2005 12 52 A 22 &lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
data temp;&lt;BR /&gt;
 set temp;&lt;BR /&gt;
 year=input(_year,best4.);&lt;BR /&gt;
 month=input(_month,best2.);&lt;BR /&gt;
 week=input(_week,best2.);&lt;BR /&gt;
 drop _:;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sort data=temp;&lt;BR /&gt;
 by stock year month week;&lt;BR /&gt;
run;&lt;BR /&gt;
data result month(drop=price current_ytd_price_sum week) year(drop=price current_mtd_price_sum month week);&lt;BR /&gt;
 set temp;&lt;BR /&gt;
 by stock year month ;&lt;BR /&gt;
 if first.month then current_mtd_price_sum=0;&lt;BR /&gt;
 if first.year then current_ytd_price_sum=0;&lt;BR /&gt;
 current_mtd_price_sum+price;&lt;BR /&gt;
 current_ytd_price_sum+price;&lt;BR /&gt;
 if last.month then output month;&lt;BR /&gt;
 if last.year then output year;&lt;BR /&gt;
 output result;&lt;BR /&gt;
run;&lt;BR /&gt;
data week(keep=stock year week previous_wtd_price_sum);&lt;BR /&gt;
 set result(rename=(price=previous_wtd_price_sum));&lt;BR /&gt;
 year=year+1;&lt;BR /&gt;
run;&lt;BR /&gt;
data month(rename=(current_mtd_price_sum=previous_mtd_price_sum));&lt;BR /&gt;
 set month;&lt;BR /&gt;
 year=year+1;&lt;BR /&gt;
run;&lt;BR /&gt;
data year(rename=(current_ytd_price_sum=previous_ytd_price_sum));&lt;BR /&gt;
 set year;&lt;BR /&gt;
 year=year+1;&lt;BR /&gt;
run;&lt;BR /&gt;
data result(where =(price is not missing));&lt;BR /&gt;
 merge result year;&lt;BR /&gt;
 by stock year;&lt;BR /&gt;
run;&lt;BR /&gt;
data result(where =(price is not missing));&lt;BR /&gt;
 merge result month;&lt;BR /&gt;
 by stock year month;&lt;BR /&gt;
run;&lt;BR /&gt;
data result(where =(price is not missing));&lt;BR /&gt;
 merge result week;&lt;BR /&gt;
 by stock year week;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Tue, 19 Apr 2011 07:29:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Addition-of-Values-for-Current-and-Previous-Values/m-p/50811#M10633</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-04-19T07:29:23Z</dc:date>
    </item>
    <item>
      <title>Re: Addition of Values for Current and Previous Values.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Addition-of-Values-for-Current-and-Previous-Values/m-p/50812#M10634</link>
      <description>Hi KSharp. Thank you for your codes. It's been very helpful in solving the problem.&lt;BR /&gt;
&lt;BR /&gt;
I have tried your codes below and I found that the Previous_YTD_Price_Sum values do not correspond to the Current_YTD_Price_Sum (which should be the correct way) as the Previous_YTD_Price_Sum only showed the total value of 1220 in every cell instead of the gradual addition of values as seen in Current_YTD_Price_Sum.&lt;BR /&gt;
&lt;BR /&gt;
Thank you once again for your guidance.&lt;BR /&gt;
&lt;BR /&gt;
KYW</description>
      <pubDate>Wed, 20 Apr 2011 01:52:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Addition-of-Values-for-Current-and-Previous-Values/m-p/50812#M10634</guid>
      <dc:creator>KYW</dc:creator>
      <dc:date>2011-04-20T01:52:54Z</dc:date>
    </item>
    <item>
      <title>Re: Addition of Values for Current and Previous Values.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Addition-of-Values-for-Current-and-Previous-Values/m-p/50813#M10635</link>
      <description>Oh.I understand what you need.How about this:&lt;BR /&gt;
[pre]&lt;BR /&gt;
data temp;&lt;BR /&gt;
input _year $  _month $ _week $ stock $ price ;&lt;BR /&gt;
cards;&lt;BR /&gt;
2004 01 01 A 21&lt;BR /&gt;
2004 01 02 A 25&lt;BR /&gt;
2004 01 03 A 22&lt;BR /&gt;
2004 01 04 A 23&lt;BR /&gt;
2004 01 05 A 24&lt;BR /&gt;
2004 02 06 A 27&lt;BR /&gt;
2004 02 07 A 25&lt;BR /&gt;
2004 02 08 A 23&lt;BR /&gt;
2004 02 09 A 23&lt;BR /&gt;
2004 03 10 A 22&lt;BR /&gt;
2004 03 11 A 22&lt;BR /&gt;
2004 03 12 A 22&lt;BR /&gt;
2004 03 13 A 26&lt;BR /&gt;
2004 03 14 A 24&lt;BR /&gt;
2004 04 15 A 23&lt;BR /&gt;
2004 04 16 A 25&lt;BR /&gt;
2004 04 17 A 22&lt;BR /&gt;
2004 04 18 A 25&lt;BR /&gt;
2004 05 19 A 22&lt;BR /&gt;
2004 05 20 A 25&lt;BR /&gt;
2004 05 21 A 21&lt;BR /&gt;
2004 05 22 A 23&lt;BR /&gt;
2004 05 23 A 21&lt;BR /&gt;
2004 06 24 A 21&lt;BR /&gt;
2004 06 25 A 22&lt;BR /&gt;
2004 06 26 A 22&lt;BR /&gt;
2004 06 27 A 23&lt;BR /&gt;
2004 07 28 A 21&lt;BR /&gt;
2004 07 29 A 24&lt;BR /&gt;
2004 07 30 A 26&lt;BR /&gt;
2004 07 31 A 27&lt;BR /&gt;
2004 07 32 A 28&lt;BR /&gt;
2004 08 33 A 27&lt;BR /&gt;
2004 08 34 A 27&lt;BR /&gt;
2004 08 35 A 26&lt;BR /&gt;
2004 08 36 A 23&lt;BR /&gt;
2004 08 37 A 24&lt;BR /&gt;
2004 09 38 A 21&lt;BR /&gt;
2004 09 39 A 25&lt;BR /&gt;
2004 09 40 A 22&lt;BR /&gt;
2004 09 41 A 23&lt;BR /&gt;
2004 10 42 A 22&lt;BR /&gt;
2004 10 43 A 25&lt;BR /&gt;
2004 10 44 A 27&lt;BR /&gt;
2004 10 45 A 24&lt;BR /&gt;
2004 11 46 A 23&lt;BR /&gt;
2004 11 47 A 21&lt;BR /&gt;
2004 11 48 A 22&lt;BR /&gt;
2004 11 49 A 20&lt;BR /&gt;
2004 12 50 A 21&lt;BR /&gt;
2004 12 51 A 25&lt;BR /&gt;
2004 12 52 A 22&lt;BR /&gt;
2005 01 01 A 21&lt;BR /&gt;
2005 01 02 A 25&lt;BR /&gt;
2005 01 03 A 22&lt;BR /&gt;
2005 01 04 A 23&lt;BR /&gt;
2005 01 05 A 24&lt;BR /&gt;
2005 02 06 A 27&lt;BR /&gt;
2005 02 07 A 25&lt;BR /&gt;
2005 02 08 A 23&lt;BR /&gt;
2005 02 09 A 23&lt;BR /&gt;
2005 03 10 A 22&lt;BR /&gt;
2005 03 11 A 22&lt;BR /&gt;
2005 03 12 A 22&lt;BR /&gt;
2005 03 13 A 26&lt;BR /&gt;
2005 03 14 A 24&lt;BR /&gt;
2005 04 15 A 23&lt;BR /&gt;
2005 04 16 A 25&lt;BR /&gt;
2005 04 17 A 22&lt;BR /&gt;
2005 04 18 A 25&lt;BR /&gt;
2005 05 19 A 22&lt;BR /&gt;
2005 05 20 A 25&lt;BR /&gt;
2005 05 21 A 21&lt;BR /&gt;
2005 05 22 A 23&lt;BR /&gt;
2005 05 23 A 21&lt;BR /&gt;
2005 06 24 A 21&lt;BR /&gt;
2005 06 25 A 22&lt;BR /&gt;
2005 06 26 A 22&lt;BR /&gt;
2005 06 27 A 23&lt;BR /&gt;
2005 07 28 A 21&lt;BR /&gt;
2005 07 29 A 24&lt;BR /&gt;
2005 07 30 A 26&lt;BR /&gt;
2005 07 31 A 27&lt;BR /&gt;
2005 07 32 A 28&lt;BR /&gt;
2005 08 33 A 27&lt;BR /&gt;
2005 08 34 A 27&lt;BR /&gt;
2005 08 35 A 26&lt;BR /&gt;
2005 08 36 A 23&lt;BR /&gt;
2005 08 37 A 24&lt;BR /&gt;
2005 09 38 A 21&lt;BR /&gt;
2005 09 39 A 25&lt;BR /&gt;
2005 09 40 A 22&lt;BR /&gt;
2005 09 41 A 23&lt;BR /&gt;
2005 10 42 A 22&lt;BR /&gt;
2005 10 43 A 25&lt;BR /&gt;
2005 10 44 A 27&lt;BR /&gt;
2005 10 45 A 24&lt;BR /&gt;
2005 11 46 A 23&lt;BR /&gt;
2005 11 47 A 21&lt;BR /&gt;
2005 11 48 A 22&lt;BR /&gt;
2005 11 49 A 20&lt;BR /&gt;
2005 12 50 A 21&lt;BR /&gt;
2005 12 51 A 25&lt;BR /&gt;
2005 12 52 A 22 &lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
data temp;&lt;BR /&gt;
 set temp;&lt;BR /&gt;
 year=input(_year,best4.);&lt;BR /&gt;
 month=input(_month,best2.);&lt;BR /&gt;
 week=input(_week,best2.);&lt;BR /&gt;
 drop _:;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc sort data=temp;&lt;BR /&gt;
 by stock year month week;&lt;BR /&gt;
run;&lt;BR /&gt;
data result month(drop=price current_ytd_price_sum week) year(drop=price current_mtd_price_sum  week);&lt;BR /&gt;
 set temp;&lt;BR /&gt;
 by stock year month ;&lt;BR /&gt;
 if first.year then current_ytd_price_sum=0;&lt;BR /&gt;
 if first.month then current_mtd_price_sum=0; &lt;BR /&gt;
 current_ytd_price_sum+price;&lt;BR /&gt;
 current_mtd_price_sum+price;&lt;BR /&gt;
 current_wtd_price_sum=price;&lt;BR /&gt;
&lt;BR /&gt;
run;&lt;BR /&gt;
data week(keep=stock year week previous_wtd_price_sum);&lt;BR /&gt;
 set result(rename=(current_wtd_price_sum=previous_wtd_price_sum));&lt;BR /&gt;
 year=year+1;&lt;BR /&gt;
run;&lt;BR /&gt;
data month(rename=(current_mtd_price_sum=previous_mtd_price_sum));&lt;BR /&gt;
 set month;&lt;BR /&gt;
 year=year+1;&lt;BR /&gt;
run;&lt;BR /&gt;
data year(rename=(current_ytd_price_sum=previous_ytd_price_sum));&lt;BR /&gt;
 set year;&lt;BR /&gt;
 year=year+1;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data result(where =(price is not missing));&lt;BR /&gt;
 merge result year;&lt;BR /&gt;
 by stock year month;&lt;BR /&gt;
run;&lt;BR /&gt;
data result(where =(price is not missing));&lt;BR /&gt;
 merge result month;&lt;BR /&gt;
 by stock year month;&lt;BR /&gt;
run;&lt;BR /&gt;
data result(where =(price is not missing));&lt;BR /&gt;
 merge result week;&lt;BR /&gt;
 by stock year week;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Ksharp</description>
      <pubDate>Wed, 20 Apr 2011 10:07:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Addition-of-Values-for-Current-and-Previous-Values/m-p/50813#M10635</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2011-04-20T10:07:53Z</dc:date>
    </item>
    <item>
      <title>Re: Addition of Values for Current and Previous Values.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Addition-of-Values-for-Current-and-Previous-Values/m-p/50814#M10636</link>
      <description>Hi KSharp. Thank you very much. This is the answer that I've been looking for so long. Yet you did it in a simple way and yet easy to understand. &lt;BR /&gt;
&lt;BR /&gt;
Really appreciate what you have been doing to help those who dabble in SAS programming, including me.</description>
      <pubDate>Thu, 21 Apr 2011 01:47:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Addition-of-Values-for-Current-and-Previous-Values/m-p/50814#M10636</guid>
      <dc:creator>KYW</dc:creator>
      <dc:date>2011-04-21T01:47:56Z</dc:date>
    </item>
  </channel>
</rss>

