<?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: Get count of records from previous month and put it next to current month in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Get-count-of-records-from-previous-month-and-put-it-next-to/m-p/894335#M353290</link>
    <description>The year is 2023 for all the months the data starts from Jan 1 2023 and for December the record count can be considered 0 according to query. &lt;BR /&gt;&lt;BR /&gt;I have already produced count of transactions in each month so my original table has &lt;BR /&gt;&lt;BR /&gt;Cust_id Month Txn_count &lt;BR /&gt;1 Jan 15 &lt;BR /&gt;1 Feb 5 &lt;BR /&gt;1 May 12 &lt;BR /&gt;1 June 4&lt;BR /&gt;2 Sept 64&lt;BR /&gt;2 Dec 6&lt;BR /&gt;3 Aug 34&lt;BR /&gt;3 Sept 2&lt;BR /&gt;3 Oct 23&lt;BR /&gt;&lt;BR /&gt;I want to show like this &lt;BR /&gt;Cust_id Month_current. Txn_count_current Month_previous Txn_count_previous &lt;BR /&gt;1 Jan 15 Dec 0&lt;BR /&gt;1 Feb 5 Jan 15&lt;BR /&gt;1 May 12 Apr 0 &lt;BR /&gt;1 June 4 May 12 &lt;BR /&gt;2 Sept 64 Aug 0 &lt;BR /&gt;2 Dec 6 Nov 0 &lt;BR /&gt;3 Aug 34 Jul 0 &lt;BR /&gt;3 Sept 2 Aug 34&lt;BR /&gt;3 Oct 23 sept 2</description>
    <pubDate>Thu, 14 Sep 2023 18:02:34 GMT</pubDate>
    <dc:creator>SAS_New_User1</dc:creator>
    <dc:date>2023-09-14T18:02:34Z</dc:date>
    <item>
      <title>Get count of records from previous month and put it next to current month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-count-of-records-from-previous-month-and-put-it-next-to/m-p/894319#M353283</link>
      <description>I want to count records from previous month and add it next to current month I.e. display all this information in a table. &lt;BR /&gt;&lt;BR /&gt;Id   CurrentMonth   Previousmonth currentCount  previouscount &lt;BR /&gt;&lt;BR /&gt;1  Jan  Dec 10 15 &lt;BR /&gt;2 Feb Jan  7 10&lt;BR /&gt;3. Mar Feb 2 7 &lt;BR /&gt;4 Apr Mar 12 2 &lt;BR /&gt;&lt;BR /&gt;Thank you &lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 14 Sep 2023 17:09:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-count-of-records-from-previous-month-and-put-it-next-to/m-p/894319#M353283</guid>
      <dc:creator>SAS_New_User1</dc:creator>
      <dc:date>2023-09-14T17:09:50Z</dc:date>
    </item>
    <item>
      <title>Re: Get count of records from previous month and put it next to current month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-count-of-records-from-previous-month-and-put-it-next-to/m-p/894321#M353285</link>
      <description>&lt;P&gt;Show what you currently have.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then show, using that example, what you expect as a result. Your current example doesn't show anyplace to get 15 for the first line.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Caution: Previous month when there is no YEAR value is very problematic. If you only have "JAN" and "DEC" in your data how do you know which "DEC" come before "JAN"??&lt;/P&gt;</description>
      <pubDate>Thu, 14 Sep 2023 17:19:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-count-of-records-from-previous-month-and-put-it-next-to/m-p/894321#M353285</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-09-14T17:19:28Z</dc:date>
    </item>
    <item>
      <title>Re: Get count of records from previous month and put it next to current month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-count-of-records-from-previous-month-and-put-it-next-to/m-p/894322#M353286</link>
      <description>Does your data have a year component?&lt;BR /&gt;</description>
      <pubDate>Thu, 14 Sep 2023 17:19:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-count-of-records-from-previous-month-and-put-it-next-to/m-p/894322#M353286</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-09-14T17:19:53Z</dc:date>
    </item>
    <item>
      <title>Re: Get count of records from previous month and put it next to current month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-count-of-records-from-previous-month-and-put-it-next-to/m-p/894324#M353288</link>
      <description>The year is 2023 for all the months the data starts from Jan 1 2023 and for December the record count can be considered 0 according to query. &lt;BR /&gt;&lt;BR /&gt;I have already produced count of transactions in each month so my original table has &lt;BR /&gt;&lt;BR /&gt;Cust_id  Month  Txn_count &lt;BR /&gt;1  Jan   15 &lt;BR /&gt;1  Feb  5 &lt;BR /&gt;1  May 12 &lt;BR /&gt;1 June 4&lt;BR /&gt;2  Sept 64&lt;BR /&gt;2 Dec 6&lt;BR /&gt;3 Aug 34&lt;BR /&gt;3 Sept 2&lt;BR /&gt;3 Oct 23&lt;BR /&gt;&lt;BR /&gt;I want to show like this &lt;BR /&gt;Cust_id  Month_current. Txn_count_current  Month_previous Txn_count_previous  &lt;BR /&gt;1  Jan   15  Dec 0&lt;BR /&gt;1  Feb  5  Jan 15&lt;BR /&gt;1  May 12  Apr  0 &lt;BR /&gt;1  June 4   May  12 &lt;BR /&gt;2  Sept  64  Aug  0 &lt;BR /&gt;2  Dec  6  Nov  0 &lt;BR /&gt;3  Aug  34  Jul  0 &lt;BR /&gt;3  Sept  2  Aug  34&lt;BR /&gt;3  Oct  23  sept  2 &lt;BR /&gt;</description>
      <pubDate>Thu, 14 Sep 2023 17:34:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-count-of-records-from-previous-month-and-put-it-next-to/m-p/894324#M353288</guid>
      <dc:creator>SAS_New_User1</dc:creator>
      <dc:date>2023-09-14T17:34:09Z</dc:date>
    </item>
    <item>
      <title>Re: Get count of records from previous month and put it next to current month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-count-of-records-from-previous-month-and-put-it-next-to/m-p/894326#M353289</link>
      <description>The year is all 2023 so year can be ignored</description>
      <pubDate>Thu, 14 Sep 2023 17:37:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-count-of-records-from-previous-month-and-put-it-next-to/m-p/894326#M353289</guid>
      <dc:creator>SAS_New_User1</dc:creator>
      <dc:date>2023-09-14T17:37:25Z</dc:date>
    </item>
    <item>
      <title>Re: Get count of records from previous month and put it next to current month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-count-of-records-from-previous-month-and-put-it-next-to/m-p/894335#M353290</link>
      <description>The year is 2023 for all the months the data starts from Jan 1 2023 and for December the record count can be considered 0 according to query. &lt;BR /&gt;&lt;BR /&gt;I have already produced count of transactions in each month so my original table has &lt;BR /&gt;&lt;BR /&gt;Cust_id Month Txn_count &lt;BR /&gt;1 Jan 15 &lt;BR /&gt;1 Feb 5 &lt;BR /&gt;1 May 12 &lt;BR /&gt;1 June 4&lt;BR /&gt;2 Sept 64&lt;BR /&gt;2 Dec 6&lt;BR /&gt;3 Aug 34&lt;BR /&gt;3 Sept 2&lt;BR /&gt;3 Oct 23&lt;BR /&gt;&lt;BR /&gt;I want to show like this &lt;BR /&gt;Cust_id Month_current. Txn_count_current Month_previous Txn_count_previous &lt;BR /&gt;1 Jan 15 Dec 0&lt;BR /&gt;1 Feb 5 Jan 15&lt;BR /&gt;1 May 12 Apr 0 &lt;BR /&gt;1 June 4 May 12 &lt;BR /&gt;2 Sept 64 Aug 0 &lt;BR /&gt;2 Dec 6 Nov 0 &lt;BR /&gt;3 Aug 34 Jul 0 &lt;BR /&gt;3 Sept 2 Aug 34&lt;BR /&gt;3 Oct 23 sept 2</description>
      <pubDate>Thu, 14 Sep 2023 18:02:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-count-of-records-from-previous-month-and-put-it-next-to/m-p/894335#M353290</guid>
      <dc:creator>SAS_New_User1</dc:creator>
      <dc:date>2023-09-14T18:02:34Z</dc:date>
    </item>
    <item>
      <title>Re: Get count of records from previous month and put it next to current month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-count-of-records-from-previous-month-and-put-it-next-to/m-p/894381#M353293</link>
      <description>&lt;P&gt;IF your data is all one year then there is no "previous month" for Jan.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Months as 3-letter abbreviations are not easy to work with in general, even worse they aren't even all the same length. They do not sort. You have to explicitly describe rules for what is prior, or following, AND you need an extra rule for every interval, 2 months prior or 2 months after that may be needed.&lt;/P&gt;
&lt;P&gt;Suggestion is create an actual DATE because SAS provides tools to examine dates, increment and display.&lt;/P&gt;
&lt;P&gt;First is provide example data in the form or a working data step. I suspect this has been mentioned before. Something like:&lt;/P&gt;
&lt;PRE&gt;data have;
   input Cust_id Month $ Txn_count;
datalines;
1 Jan 15
1 Feb 5
1 May 12
1 June 4
2 Sept 64
2 Dec 6
3 Aug 34
3 Sept 2
3 Oct 23
;
&lt;/PRE&gt;
&lt;P&gt;With that then we have one possible solution (there are others)&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   by cust_id notsorted;
   datevar = input(cats('01',substr(month,1,3),'2023'),date9.);
   format datevar monname3.;
   month_previous = intnx('month',datevar,-1,'b');
   format month_previous monname3.;
   txn_count_previous = lag(txn_count);
   if first.cust_id then txn_count_previous = 0;
run;&lt;/PRE&gt;
&lt;P&gt;We create a SAS date value by creating a string that is in the form of ddMONyyyy, or 01JAN2023 and read that with an informat designed to do such. Note: SEPT is right out (why? only month with 4 characters).&lt;/P&gt;
&lt;P&gt;The format statement makes the default appearance 3 letters such as Jan. With that value in hand we can use the SAS INTNX function to increment the value, in this case by month, -1 is "previous", and 'b' says beginning of the month.&lt;/P&gt;
&lt;P&gt;Noticing that you changed the description of the problem again by introducing custid as variable to consider as a group, meaning the count assignment I add the BY Cust_Id. When you process data in a data step SAS provides automatic variables to indicate whether a particular observation is from the first or last record of a group. The variables accessed using First.variablename or Last.variablename and have values of 1 (for Yes, is first or is last) or 0. So we can conditionally do things at the start or the end of the group. The NOTSORTED means treat the values as group but may not be actual sort order as the BY statement expects without the option.&lt;/P&gt;
&lt;P&gt;The LAG function gets the value from the previous record. Conditionally we assign the desired value of 0 when the first of the cust_id values is reached.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I left the new Datevar in the data in case you need to do anything that uses "month" in actual month order.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Sep 2023 20:43:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-count-of-records-from-previous-month-and-put-it-next-to/m-p/894381#M353293</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-09-14T20:43:04Z</dc:date>
    </item>
    <item>
      <title>Re: Get count of records from previous month and put it next to current month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Get-count-of-records-from-previous-month-and-put-it-next-to/m-p/894400#M353300</link>
      <description>&lt;P&gt;If&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;you really want to avoid generating data variables,&lt;BR /&gt;and&lt;/LI&gt;
&lt;LI&gt;you know that you never have a gap of more than 12 months for a given cust_id&lt;BR /&gt;&lt;BR /&gt;then&lt;/LI&gt;
&lt;LI&gt;you can utilize a two-dimensional array, the first row for Jan through Dec abbreviations, and the second row for Dec through Jan.&amp;nbsp; I.e. the second row names months that precede the months present in the corresponding column of the first row:&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by cust_id;
  array mon_list {2,12} $3
    _temporary_ ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec',
                 'Dec','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov');
  cur_month=propcase(substr(month,1,3));
  prv_month=mon_list{2,whichc(cur_month,of mon_list{*})};

  txn_count_previous = ifn(lag(cur_month)=prv_month and first.cust_id=0,lag(txn_count),0);

run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 Sep 2023 00:08:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Get-count-of-records-from-previous-month-and-put-it-next-to/m-p/894400#M353300</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-09-15T00:08:04Z</dc:date>
    </item>
  </channel>
</rss>

