<?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: Geting Previous Month Value with Joins in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Geting-Previous-Month-Value-with-Joins/m-p/737308#M229850</link>
    <description>&lt;P&gt;So what do you expect for the output? Not clear.&lt;/P&gt;</description>
    <pubDate>Tue, 27 Apr 2021 16:15:48 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2021-04-27T16:15:48Z</dc:date>
    <item>
      <title>Geting Previous Month Value with Joins</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Geting-Previous-Month-Value-with-Joins/m-p/737305#M229847</link>
      <description>&lt;P&gt;Hello community,&amp;nbsp;&lt;BR /&gt;I am currently facing an issue getting previous month end value. Please Help with this E.T.L logic I have been on this for the past 2 days...&lt;BR /&gt;&lt;BR /&gt;My objective is to get to this Output:&lt;/P&gt;
&lt;P&gt;i.e in this example for every date (20 and 21 Jan 2020) the PrevMonthEndValue should be equal to the value for the date (31 Dec 2019)&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;date	    Value	   PrevMonthEndValue
31DEC2019	350	       .
20JAN2020	550	       350
21JAN2020	200	       350&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;&amp;nbsp;I have this dataset below;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table_values;
 INPUT date currency $ value ;
CARDS;
21914 USD 200
21914 USD 100
21914 USD 50
21934 USD 50
21934 USD 500
21935 USD 200
;
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;And I need to full join the dataset to get every position so as to get a valid total:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table work.result_aggregation as
   select
      COALESCE(t_left.date, (intnx('month', t_left.date,1,'E') ), t_right.date) as t_date format=date9.,
      COALESCE(t_left.Currency,t_right.Currency ) as Currency,
	  t_left.date as left_date format=date9.,
	  t_right.date as right_date format=date9.,
      t_left.value as Value,
      t_right.value as PrevMonthEndValue
   from
      work.table_values as t_left full join 
      work.table_values as t_right
         on
         (
            intnx('month',t_left.date,-1,'E') = t_right.date
            and t_left.Currency = t_right.Currency
         )
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I then have a sum for the total&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table work.summarized as
      select
         t_date,
         (SUM(Value)) as Value,
         (SUM(PrevMonthEndValue)) as PrevMonthEndValue
   from work.result_aggregation
   group by
      t_date
   order by
      t_date
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;And this is my output...&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PhiSlogan_0-1619539304341.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/58727i5AC3C6617078828D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="PhiSlogan_0-1619539304341.png" alt="PhiSlogan_0-1619539304341.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Apr 2021 07:39:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Geting-Previous-Month-Value-with-Joins/m-p/737305#M229847</guid>
      <dc:creator>PhiSlogan</dc:creator>
      <dc:date>2021-04-28T07:39:00Z</dc:date>
    </item>
    <item>
      <title>Re: Geting Previous Month Value with Joins</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Geting-Previous-Month-Value-with-Joins/m-p/737308#M229850</link>
      <description>&lt;P&gt;So what do you expect for the output? Not clear.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Apr 2021 16:15:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Geting-Previous-Month-Value-with-Joins/m-p/737308#M229850</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-04-27T16:15:48Z</dc:date>
    </item>
    <item>
      <title>Re: Geting Previous Month Value with Joins</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Geting-Previous-Month-Value-with-Joins/m-p/737311#M229851</link>
      <description>&lt;P&gt;To expand on&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;'s succinct but very valid point ... its very hard (actually, its impossible) to generalize solutions when you only present two months to work with. I get the feeling that someone (like me) will present a solution that works on these two months, but not on a case when there are three or more months (and so I have wasted my time working on just your solution from data that covers only two months). And so, you need to present an example which covers the variety of situations that might be encountered with your real data, and so we can generalize from more than two months.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyway, here is my solution:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table_values;
 INPUT date currency $ value ;
 endmonth=intnx('month',date,0,'e');
CARDS;
21914 USD 200
21914 USD 100
21914 USD 50
21934 USD 50
21934 USD 500
21935 USD 200
;
run; 
proc summary nway data=table_values;
    class date;
    var value;
    id endmonth;
    output out=sums sum=monthsum;
run;
proc sql;
    create table want as select
    a.date,a.monthsum,b.monthsum as prevmonthsum
    from sums as a left join sums as b
    on a.endmonth=intnx('month',b.endmonth,1,'e')
    order by a.date;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Apr 2021 16:36:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Geting-Previous-Month-Value-with-Joins/m-p/737311#M229851</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-04-27T16:36:34Z</dc:date>
    </item>
    <item>
      <title>Re: Geting Previous Month Value with Joins</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Geting-Previous-Month-Value-with-Joins/m-p/737321#M229853</link>
      <description>I expect to get the value for the  last day of the previous month&lt;BR /&gt;</description>
      <pubDate>Tue, 27 Apr 2021 16:34:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Geting-Previous-Month-Value-with-Joins/m-p/737321#M229853</guid>
      <dc:creator>PhiSlogan</dc:creator>
      <dc:date>2021-04-27T16:34:56Z</dc:date>
    </item>
    <item>
      <title>Re: Geting Previous Month Value with Joins</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Geting-Previous-Month-Value-with-Joins/m-p/737331#M229860</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/244630"&gt;@PhiSlogan&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I expect to get the value for the last day of the previous month&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;But there are 3 values in your example data for the last day. And since you show a result that is probably incorrect it is not clear what you expect.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Apr 2021 17:03:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Geting-Previous-Month-Value-with-Joins/m-p/737331#M229860</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-04-27T17:03:36Z</dc:date>
    </item>
    <item>
      <title>Re: Geting Previous Month Value with Joins</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Geting-Previous-Month-Value-with-Joins/m-p/737503#M229945</link>
      <description>Please help me understand where it is not clear for you... This output has 2 values as the *last day of last month values* not 3.. The 1st value is the current  value for the 31Dec2019.  &lt;BR /&gt;&lt;BR /&gt;date	                Value	   PrevMonthEndValue&lt;BR /&gt;31DEC2019	350	       .&lt;BR /&gt;20JAN2020	550	       350&lt;BR /&gt;21JAN2020	200	       350</description>
      <pubDate>Wed, 28 Apr 2021 07:44:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Geting-Previous-Month-Value-with-Joins/m-p/737503#M229945</guid>
      <dc:creator>PhiSlogan</dc:creator>
      <dc:date>2021-04-28T07:44:45Z</dc:date>
    </item>
    <item>
      <title>Re: Geting Previous Month Value with Joins</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Geting-Previous-Month-Value-with-Joins/m-p/737507#M229946</link>
      <description>&lt;P&gt;This does what you want:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table_values;
input date currency $ value ;
datalines;
21914 USD 200
21914 USD 100
21914 USD 50
21934 USD 50
21934 USD 500
21935 USD 200
;

data want;
set table_values (
);
by date;
set
  table_values (
    firstobs=2
    keep=date
    rename=(date=_date)
  )
  table_values (
    obs=1
    keep=date
    rename=(date=_date)
  )
;
retain PrevMonthEndValue _value;
_value + value;
if last.date
then do;
  value = _value;
  output;
  if month(date) ne month(_date)
  then do;
    PrevMonthEndValue = _value;
  end;
  _value = 0;
end;
drop _value _date;
format _date date yymmdd10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;date	currency	value	PrevMonthEndValue
2019-12-31	USD	350	.
2020-01-20	USD	550	350
2020-01-21	USD	200	350&lt;/PRE&gt;</description>
      <pubDate>Wed, 28 Apr 2021 08:49:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Geting-Previous-Month-Value-with-Joins/m-p/737507#M229946</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-04-28T08:49:35Z</dc:date>
    </item>
  </channel>
</rss>

