<?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: How do I accumulate values in a column within in a year (period = months) having multiple years? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-accumulate-values-in-a-column-within-in-a-year-period/m-p/716106#M221255</link>
    <description>&lt;P&gt;You can use the retain statement to accumulate a variable or a sum statement. This topic is discussed in the SAS Programming 2 class in detail. I would recommend you take this course.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an example to your question but only using one of the variables var1 by year.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;DATA WORK.temp; &lt;BR /&gt;Infile datalines delimiter=','; &lt;BR /&gt;INPUT YearMonth $ var1 var2 Desired_Accu1 Desired_Accu2&lt;BR /&gt;;&lt;BR /&gt;year=substr(yearmonth,1,4);&lt;BR /&gt;CARDS;&lt;BR /&gt;2019-01,100,500,100,500&lt;BR /&gt;2019-02,200,600,300,1100&lt;BR /&gt;2019-03,300,700,600,1800&lt;BR /&gt;2019-04,400,100,1000,1900&lt;BR /&gt;2019-05,500,200,1500,2100&lt;BR /&gt;2019-06,600,300,2100,2400&lt;BR /&gt;2019-07,700,400,2800,2800&lt;BR /&gt;2019-08,800,500,3600,3300&lt;BR /&gt;2019-09,100,500,3700,3800&lt;BR /&gt;2019-10,200,600,3900,4400&lt;BR /&gt;2019-11,300,700,4200,5100&lt;BR /&gt;2019-12,400,100,4600,5200&lt;BR /&gt;2020-01,500,200,500,200&lt;BR /&gt;2020-02,600,300,1100,500&lt;BR /&gt;2020-03,700,400,1800,900&lt;BR /&gt;2020-04,800,500,2600,1400&lt;BR /&gt;2020-05,100,500,2700,1900&lt;BR /&gt;2020-06,200,600,2900,2500&lt;BR /&gt;2020-07,300,700,3200,3200&lt;BR /&gt;2020-08,400,100,3600,3300&lt;BR /&gt;2020-09,500,200,4100,3500&lt;BR /&gt;2020-10,600,300,4700,3800&lt;BR /&gt;2020-11,700,400,5400,4200&lt;BR /&gt;2020-12,800,500,6200,4700&lt;BR /&gt;2021-01,100,500,100,500&lt;BR /&gt;2021-02,50,300,150,800&lt;/P&gt;
&lt;P&gt;;&lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;proc sort data=WORK.temp;&lt;BR /&gt;by Year;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;Data work.temp(keep=year var1 ret_var1);&lt;BR /&gt;set work.temp;&lt;BR /&gt;by year;&lt;BR /&gt;if first.year=1 then ret_var1=0;&lt;BR /&gt;ret_var1+var1;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 02 Feb 2021 15:58:30 GMT</pubDate>
    <dc:creator>CarmineVerrell</dc:creator>
    <dc:date>2021-02-02T15:58:30Z</dc:date>
    <item>
      <title>How do I accumulate values in a column within in a year (period = months) having multiple years?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-accumulate-values-in-a-column-within-in-a-year-period/m-p/716103#M221254</link>
      <description>&lt;P&gt;I have been using the following, which is quite neat when you want to sum values in a column.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;IF FIRST.var1 THEN ACC_var1=var1;&lt;/P&gt;&lt;P&gt;ELSE ACC_var+var1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, I only have the skills to do it when the dataset only contains 12 months. ^^&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;How do I accumulate the values within in a year (period = months) when I have multiple years?&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For the program code attached I would like to accumulated the 12 months for 2019 and 2020 and the current ones for 2021 for both var1 and var2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Should I use a DO-loop/some sort of lag solution or is there an another way to use First./ Last.?&lt;/P&gt;&lt;P&gt;I am open for any suggestion. Many thanks!&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WORK.temp; 
Infile datalines delimiter=','; 
INPUT YearMonth $ var1 var2 Desired_Accu1 Desired_Accu2
;
CARDS;
2019-01,100,500,100,500
2019-02,200,600,300,1100
2019-03,300,700,600,1800
2019-04,400,100,1000,1900
2019-05,500,200,1500,2100
2019-06,600,300,2100,2400
2019-07,700,400,2800,2800
2019-08,800,500,3600,3300
2019-09,100,500,3700,3800
2019-10,200,600,3900,4400
2019-11,300,700,4200,5100
2019-12,400,100,4600,5200
2020-01,500,200,500,200
2020-02,600,300,1100,500
2020-03,700,400,1800,900
2020-04,800,500,2600,1400
2020-05,100,500,2700,1900
2020-06,200,600,2900,2500
2020-07,300,700,3200,3200
2020-08,400,100,3600,3300
2020-09,500,200,4100,3500
2020-10,600,300,4700,3800
2020-11,700,400,5400,4200
2020-12,800,500,6200,4700
2021-01,100,500,100,500
2021-02,50,300,150,800

;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Feb 2021 15:42:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-accumulate-values-in-a-column-within-in-a-year-period/m-p/716103#M221254</guid>
      <dc:creator>Pili1100</dc:creator>
      <dc:date>2021-02-02T15:42:57Z</dc:date>
    </item>
    <item>
      <title>Re: How do I accumulate values in a column within in a year (period = months) having multiple years?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-accumulate-values-in-a-column-within-in-a-year-period/m-p/716106#M221255</link>
      <description>&lt;P&gt;You can use the retain statement to accumulate a variable or a sum statement. This topic is discussed in the SAS Programming 2 class in detail. I would recommend you take this course.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an example to your question but only using one of the variables var1 by year.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;DATA WORK.temp; &lt;BR /&gt;Infile datalines delimiter=','; &lt;BR /&gt;INPUT YearMonth $ var1 var2 Desired_Accu1 Desired_Accu2&lt;BR /&gt;;&lt;BR /&gt;year=substr(yearmonth,1,4);&lt;BR /&gt;CARDS;&lt;BR /&gt;2019-01,100,500,100,500&lt;BR /&gt;2019-02,200,600,300,1100&lt;BR /&gt;2019-03,300,700,600,1800&lt;BR /&gt;2019-04,400,100,1000,1900&lt;BR /&gt;2019-05,500,200,1500,2100&lt;BR /&gt;2019-06,600,300,2100,2400&lt;BR /&gt;2019-07,700,400,2800,2800&lt;BR /&gt;2019-08,800,500,3600,3300&lt;BR /&gt;2019-09,100,500,3700,3800&lt;BR /&gt;2019-10,200,600,3900,4400&lt;BR /&gt;2019-11,300,700,4200,5100&lt;BR /&gt;2019-12,400,100,4600,5200&lt;BR /&gt;2020-01,500,200,500,200&lt;BR /&gt;2020-02,600,300,1100,500&lt;BR /&gt;2020-03,700,400,1800,900&lt;BR /&gt;2020-04,800,500,2600,1400&lt;BR /&gt;2020-05,100,500,2700,1900&lt;BR /&gt;2020-06,200,600,2900,2500&lt;BR /&gt;2020-07,300,700,3200,3200&lt;BR /&gt;2020-08,400,100,3600,3300&lt;BR /&gt;2020-09,500,200,4100,3500&lt;BR /&gt;2020-10,600,300,4700,3800&lt;BR /&gt;2020-11,700,400,5400,4200&lt;BR /&gt;2020-12,800,500,6200,4700&lt;BR /&gt;2021-01,100,500,100,500&lt;BR /&gt;2021-02,50,300,150,800&lt;/P&gt;
&lt;P&gt;;&lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;proc sort data=WORK.temp;&lt;BR /&gt;by Year;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;Data work.temp(keep=year var1 ret_var1);&lt;BR /&gt;set work.temp;&lt;BR /&gt;by year;&lt;BR /&gt;if first.year=1 then ret_var1=0;&lt;BR /&gt;ret_var1+var1;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Feb 2021 15:58:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-accumulate-values-in-a-column-within-in-a-year-period/m-p/716106#M221255</guid>
      <dc:creator>CarmineVerrell</dc:creator>
      <dc:date>2021-02-02T15:58:30Z</dc:date>
    </item>
    <item>
      <title>Re: How do I accumulate values in a column within in a year (period = months) having multiple years?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-accumulate-values-in-a-column-within-in-a-year-period/m-p/716107#M221256</link>
      <description>&lt;P&gt;Create a new variable called YEAR that you can use in your BY statement. You can create it by taking the first four characters of your year month variable.&lt;/P&gt;
&lt;P&gt;Now you have a variable you can use with your FIRST and LAST.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WORK.temp; 
Infile datalines delimiter=','; 
INPUT YearMonth $ var1 var2 Desired_Accu1 Desired_Accu2
;
Year = substr(yearMonth, 1, 4);
....

run;

data want;
set temp;
by year;
if first.year then do; 
       accu1 = var1; 
       accu2 = var2;
 end;
else do;
    accu1 = sum(accu1, var1);
   accu2 = sum(accu2, var2);
end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or if you can assume you will have complete data, every year/month, you can use the first month as your condition instead, no new variable needed this way.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if substr(yearMonth, 6, 2) = '01' then .....;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/363831"&gt;@Pili1100&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have been using the following, which is quite neat when you want to sum values in a column.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;IF FIRST.var1 THEN ACC_var1=var1;&lt;/P&gt;
&lt;P&gt;ELSE ACC_var+var1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, I only have the skills to do it when the dataset only contains 12 months. ^^&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;How do I accumulate the values within in a year (period = months) when I have multiple years?&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the program code attached I would like to accumulated the 12 months for 2019 and 2020 and the current ones for 2021 for both var1 and var2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Should I use a DO-loop/some sort of lag solution or is there an another way to use First./ Last.?&lt;/P&gt;
&lt;P&gt;I am open for any suggestion. Many thanks!&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WORK.temp; 
Infile datalines delimiter=','; 
INPUT YearMonth $ var1 var2 Desired_Accu1 Desired_Accu2
;
CARDS;
2019-01,100,500,100,500
2019-02,200,600,300,1100
2019-03,300,700,600,1800
2019-04,400,100,1000,1900
2019-05,500,200,1500,2100
2019-06,600,300,2100,2400
2019-07,700,400,2800,2800
2019-08,800,500,3600,3300
2019-09,100,500,3700,3800
2019-10,200,600,3900,4400
2019-11,300,700,4200,5100
2019-12,400,100,4600,5200
2020-01,500,200,500,200
2020-02,600,300,1100,500
2020-03,700,400,1800,900
2020-04,800,500,2600,1400
2020-05,100,500,2700,1900
2020-06,200,600,2900,2500
2020-07,300,700,3200,3200
2020-08,400,100,3600,3300
2020-09,500,200,4100,3500
2020-10,600,300,4700,3800
2020-11,700,400,5400,4200
2020-12,800,500,6200,4700
2021-01,100,500,100,500
2021-02,50,300,150,800

;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Feb 2021 15:59:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-accumulate-values-in-a-column-within-in-a-year-period/m-p/716107#M221256</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-02-02T15:59:27Z</dc:date>
    </item>
    <item>
      <title>Re: How do I accumulate values in a column within in a year (period = months) having multiple years?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-accumulate-values-in-a-column-within-in-a-year-period/m-p/716194#M221295</link>
      <description>&lt;P&gt;Thank you!&lt;/P&gt;&lt;P&gt;It worked like a charm!&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":beaming_face_with_smiling_eyes:"&gt;😁&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Of course it helps to change to BY Year. If I wasn't completely blocked I should have been able to figure it out.&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":thinking_face:"&gt;🤔&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Feb 2021 19:42:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-accumulate-values-in-a-column-within-in-a-year-period/m-p/716194#M221295</guid>
      <dc:creator>Pili1100</dc:creator>
      <dc:date>2021-02-02T19:42:51Z</dc:date>
    </item>
  </channel>
</rss>

