<?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: Do a calculation for each month in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Do-a-calculation-for-each-month/m-p/810826#M319765</link>
    <description>&lt;P&gt;This can be done in a single data step, by reading each ID twice, once to get the counts, second time to output the original data with those counts.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs;
input ID	Date :yymmdd12.;
format Date yymmdd10.;
cards;
1	2016-04-15
1	2016-04-20
3	2016-04-29
3	2016-04-21
3	2016-04-25
4	2017-12-17
;

data want (drop=_: nxt_:);
  do until (id^=nxt_id);
    do _ntrans=1 by 1 until (intck('month',date,nxt_date)^=0 or id^=nxt_id);
      merge have
            have (firstobs=2 keep=id date rename=(id=nxt_id date=nxt_date));
      array mnths{4:12} april_trans _unwanted5-_unwanted11 dec_trans;
    end;
    mnths{month(date)}=_ntrans;
  end;

  do _m=4,12; if  mnths{_m}=. then mnths{_m}=0; end;

  do until (last.id);
    set have;
    by id notsorted;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This program assumes that the data are grouped by id and within id, grouped by month.&amp;nbsp; The ID's (and the months within ID's) need not be in ascending order.&amp;nbsp; The technique in determining month and/or id boundaries is in the merge statement, using the "firstobs=2" parameter for one of the merged data sequences.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It uses the ARRAY statement to index the array MNTHS by month number.&amp;nbsp; Since you only want April and December, the array has lower index of 4 and upper index of 12.&amp;nbsp; And it has a bunch of unwanted variables as placeholders for May through November.&amp;nbsp; &amp;nbsp;If you want other months just make changes to the variable names and/or array index range. And modify the "do _m=4,12;" loop accordingly.&amp;nbsp; It wouldn't be hard to use the array for months ranging over multiple years - just add a second dimension for year.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 30 Apr 2022 16:50:57 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2022-04-30T16:50:57Z</dc:date>
    <item>
      <title>Do a calculation for each month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-a-calculation-for-each-month/m-p/810629#M319675</link>
      <description>&lt;P&gt;I have a table of the form:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="243"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="83"&gt;Customer ID&lt;/TD&gt;
&lt;TD width="160"&gt;Transaction Date for Buy&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;2016-04-15&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;2016-04-20&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;2016-04-29&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;2016-04-21&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;2016-04-25&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;2017-12-17&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From this data, how would I get a summary of the type:&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="653"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="82.9688px" height="58px"&gt;Customer ID&lt;/TD&gt;
&lt;TD width="159.746px" height="58px"&gt;Transaction Date for Buy&lt;/TD&gt;
&lt;TD width="186.543px" height="58px"&gt;Number of buys in april 2016&lt;/TD&gt;
&lt;TD width="222.48px" height="58px"&gt;Number of buys in december 2017&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="82.9688px" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="159.746px" height="30px"&gt;2016-04-15&lt;/TD&gt;
&lt;TD width="186.543px" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="222.48px" height="30px"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="82.9688px" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="159.746px" height="30px"&gt;2016-04-20&lt;/TD&gt;
&lt;TD width="186.543px" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="222.48px" height="30px"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="82.9688px" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="159.746px" height="30px"&gt;2016-04-29&lt;/TD&gt;
&lt;TD width="186.543px" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="222.48px" height="30px"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="82.9688px" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="159.746px" height="30px"&gt;2016-04-21&lt;/TD&gt;
&lt;TD width="186.543px" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="222.48px" height="30px"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="82.9688px" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="159.746px" height="30px"&gt;2016-04-25&lt;/TD&gt;
&lt;TD width="186.543px" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="222.48px" height="30px"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="82.9688px" height="30px"&gt;4&lt;/TD&gt;
&lt;TD width="159.746px" height="30px"&gt;2017-12-17&lt;/TD&gt;
&lt;TD width="186.543px" height="30px"&gt;0&lt;/TD&gt;
&lt;TD width="222.48px" height="30px"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&lt;BR /&gt;So I would like to get the number of buys for each month uptill to today, starting at say january 2021. So there will be very many columns in my real example.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any advice?&lt;/P&gt;</description>
      <pubDate>Fri, 29 Apr 2022 13:29:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-a-calculation-for-each-month/m-p/810629#M319675</guid>
      <dc:creator>SasStatistics</dc:creator>
      <dc:date>2022-04-29T13:29:56Z</dc:date>
    </item>
    <item>
      <title>Re: Do a calculation for each month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-a-calculation-for-each-month/m-p/810650#M319683</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs;
input ID	Date :yymmdd12.;
format Date yymmdd10.;
cards;
1	2016-04-15
1	2016-04-20
3	2016-04-29
3	2016-04-21
3	2016-04-25
4	2017-12-17
;

proc freq data=have noprint;
table id*date/out=want list;
format date yymmn6.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Apr 2022 14:00:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-a-calculation-for-each-month/m-p/810650#M319683</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-04-29T14:00:28Z</dc:date>
    </item>
    <item>
      <title>Re: Do a calculation for each month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-a-calculation-for-each-month/m-p/810796#M319758</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/381436"&gt;@SasStatistics&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following code does what you need&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs;
input ID	Date :yymmdd12.;
format Date yymmdd10.;
cards;
1	2016-04-15
1	2016-04-20
3	2016-04-29
3	2016-04-21
3	2016-04-25
4	2017-12-17
;
proc sql;
create table have2 as 
select id,date, year(date) as Year, count(id) as count
from have
group by year(date), id
order by id, date;
run;
proc transpose data=have2 out=want (drop= _NAME_) ;
by id date;
id year;
var count;
run;
data want;
   set want;
   array change _numeric_;
        do over change;
            if change=. then change=0;
        end;
 run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The out put will be as you wanted.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Sajid01_1-1651321144150.png" style="width: 385px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/71011iC6556FE4DF80FFD1/image-dimensions/385x278?v=v2" width="385" height="278" role="button" title="Sajid01_1-1651321144150.png" alt="Sajid01_1-1651321144150.png" /&gt;&lt;/span&gt;&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>Sat, 30 Apr 2022 12:19:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-a-calculation-for-each-month/m-p/810796#M319758</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2022-04-30T12:19:26Z</dc:date>
    </item>
    <item>
      <title>Re: Do a calculation for each month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Do-a-calculation-for-each-month/m-p/810826#M319765</link>
      <description>&lt;P&gt;This can be done in a single data step, by reading each ID twice, once to get the counts, second time to output the original data with those counts.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs;
input ID	Date :yymmdd12.;
format Date yymmdd10.;
cards;
1	2016-04-15
1	2016-04-20
3	2016-04-29
3	2016-04-21
3	2016-04-25
4	2017-12-17
;

data want (drop=_: nxt_:);
  do until (id^=nxt_id);
    do _ntrans=1 by 1 until (intck('month',date,nxt_date)^=0 or id^=nxt_id);
      merge have
            have (firstobs=2 keep=id date rename=(id=nxt_id date=nxt_date));
      array mnths{4:12} april_trans _unwanted5-_unwanted11 dec_trans;
    end;
    mnths{month(date)}=_ntrans;
  end;

  do _m=4,12; if  mnths{_m}=. then mnths{_m}=0; end;

  do until (last.id);
    set have;
    by id notsorted;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This program assumes that the data are grouped by id and within id, grouped by month.&amp;nbsp; The ID's (and the months within ID's) need not be in ascending order.&amp;nbsp; The technique in determining month and/or id boundaries is in the merge statement, using the "firstobs=2" parameter for one of the merged data sequences.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It uses the ARRAY statement to index the array MNTHS by month number.&amp;nbsp; Since you only want April and December, the array has lower index of 4 and upper index of 12.&amp;nbsp; And it has a bunch of unwanted variables as placeholders for May through November.&amp;nbsp; &amp;nbsp;If you want other months just make changes to the variable names and/or array index range. And modify the "do _m=4,12;" loop accordingly.&amp;nbsp; It wouldn't be hard to use the array for months ranging over multiple years - just add a second dimension for year.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 30 Apr 2022 16:50:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Do-a-calculation-for-each-month/m-p/810826#M319765</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-04-30T16:50:57Z</dc:date>
    </item>
  </channel>
</rss>

