<?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: First, last and where clauses in proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766135#M242763</link>
    <description>&lt;P&gt;Thank you, Tom !&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first line is right but unfortunately I have 0, when the dates are same :&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MarieT_0-1630865936384.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/63317iC5B83E4822C4F8CB/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MarieT_0-1630865936384.png" alt="MarieT_0-1630865936384.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;When the date are same I summirize without substraction, the result for T23900399 is 47506,40&lt;/P&gt;
&lt;P&gt;I joined the tested data.&lt;/P&gt;
&lt;P&gt;The code is :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

proc sql;
create table want as
  select no_police
       , min(grand_total - case when (date=lastdate) then total_par_police else 0 end) as total
  from
    (
      select *
           , sum(total_par_police) as grand_total
           , max(date) as lastdate format=yymmdd10.
      from
      (
        select no_police,datepart(d_effet) as date format=yymmdd10. 
             , sum(abs(mt_brut_cie)) as total_par_police
          from PRESTATIONS2
          group by no_police,date
      )
      group by no_police
    )
  group by no_police
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thank you for your help !&lt;/P&gt;</description>
    <pubDate>Sun, 05 Sep 2021 18:27:56 GMT</pubDate>
    <dc:creator>SASdevAnneMarie</dc:creator>
    <dc:date>2021-09-05T18:27:56Z</dc:date>
    <item>
      <title>First, last and where clauses in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766046#M242719</link>
      <description>Hello Experts,&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;I would like to make the sum on my&amp;amp;nbsp;MT_BRUT_CIE column, and, if I have the different dates for the MT_BRUT_CIE I would like to subtract the most recent&amp;amp;nbsp;MT_BRUT_CIE.&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;My code is :&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table test as select a.*,&lt;BR /&gt;sum(abs(a.MT_BRUT_CIE))as cumul_vers, count(distinct(a.D_EFFET)) as nombre,&lt;BR /&gt;max(datepart(a.D_EFFET)) as date format ddmmyy10.&lt;BR /&gt;/*cumul_vers-(abs(a.MT_BRUT_CIE) where date=datepart(a.D_EFFET)) and nombre&amp;amp;gt;1*/&lt;BR /&gt;from PRESTA as a&lt;BR /&gt;group by NO_POLICE;&lt;BR /&gt;quit;&lt;BR /&gt;I'm wondering how to rewrite the line between the “*” because it doesn't work.&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;I joined the data.&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;Thank you very much for your help !&lt;BR /&gt;&amp;amp;nbsp;&lt;BR /&gt;&amp;amp;nbsp;</description>
      <pubDate>Sat, 04 Sep 2021 19:22:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766046#M242719</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2021-09-04T19:22:53Z</dc:date>
    </item>
    <item>
      <title>Re: First, last and where clauses in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766047#M242720</link>
      <description>&lt;P&gt;We can't see the green.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 04 Sep 2021 19:15:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766047#M242720</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2021-09-04T19:15:20Z</dc:date>
    </item>
    <item>
      <title>Re: First, last and where clauses in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766048#M242721</link>
      <description>The line between the «&amp;nbsp;*&amp;nbsp;» in my code.</description>
      <pubDate>Sat, 04 Sep 2021 19:20:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766048#M242721</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2021-09-04T19:20:06Z</dc:date>
    </item>
    <item>
      <title>Re: First, last and where clauses in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766051#M242722</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/286185"&gt;@SASdevAnneMarie&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think it will work if you add the "calculated" keyword, like here:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*-------------------------------------------------------------------
   Referring to a Calculated Column by Alias
-------------------------------------------------------------------*/
proc sql outobs=12;
   title 'Range of High and Low Temperatures in Celsius';
      select City, (AvgHigh - 32) * 5/9 as HighC format=5.1, 
                   (AvgLow  - 32) * 5/9 as LowC  format=5.1,
                   (calculated HighC - calculated LowC) as Range format=4.1
   from sql.worldtemps;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Sat, 04 Sep 2021 20:09:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766051#M242722</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-09-04T20:09:24Z</dc:date>
    </item>
    <item>
      <title>Re: First, last and where clauses in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766053#M242724</link>
      <description>&lt;P&gt;What does it say in the log?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 04 Sep 2021 20:13:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766053#M242724</guid>
      <dc:creator>tarheel13</dc:creator>
      <dc:date>2021-09-04T20:13:24Z</dc:date>
    </item>
    <item>
      <title>Re: First, last and where clauses in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766055#M242725</link>
      <description>&lt;P&gt;By the code between the * do you mean this commented out code?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;cumul_vers-(abs(a.MT_BRUT_CIE) where date=datepart(a.D_EFFET)) and nombre&amp;gt;1&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What do you think that code means?&lt;/P&gt;</description>
      <pubDate>Sat, 04 Sep 2021 20:33:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766055#M242725</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-04T20:33:30Z</dc:date>
    </item>
    <item>
      <title>Re: First, last and where clauses in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766056#M242726</link>
      <description>&lt;P&gt;Is this the example data?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input (D_EFFET D_VALO) (:datetime.) NO_POLICE :$20. MT_BRUT_CIE cumul_vers nombre date :ddmmyy. ;
  format D_EFFET D_VALO datetime19. date yymmdd10. ;
cards;
20APR2010:00:00:00 29APR2010:09:02:02 I99000001 -40000 1013137.23 3 02/02/2018
21FEB2013:00:00:00 27FEB2013:09:29:19 I99000001 -873137.23 1013137.23 3 02/02/2018
02FEB2018:00:00:00 20FEB2018:12:02:35 I99000001 -100000 1013137.23 3 02/02/2018
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What is the expect output from that data?&lt;/P&gt;</description>
      <pubDate>Sat, 04 Sep 2021 20:35:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766056#M242726</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-04T20:35:02Z</dc:date>
    </item>
    <item>
      <title>Re: First, last and where clauses in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766057#M242727</link>
      <description>Thank you for the message, Tom. I would like to sum the MT_BRUT_CIE and subtract the most recent MT_BRUTE_CIE (the date is 20FEB2018, the amount is 100,000. I would like to do 1013137,23-100,000. I have also created the column Nombre, because some times the D_EFFET is same, in this case I don’t perform the substrate, only the sum.</description>
      <pubDate>Sat, 04 Sep 2021 21:39:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766057#M242727</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2021-09-04T21:39:50Z</dc:date>
    </item>
    <item>
      <title>Re: First, last and where clauses in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766058#M242728</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/286185"&gt;@SASdevAnneMarie&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank you for the message, Tom. I would like to sum the MT_BRUT_CIE and subtract the most recent MT_BRUTE_CIE (the date is 20FEB2018, the amount is 100,000. I would like to do 1013137,23-100,000. I have also created the column Nombre, because some times the D_EFFET is same, in this case I don’t perform the substrate, only the sum.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;First and Last is not a concept that SQL understands.&lt;/P&gt;
&lt;P&gt;Why not use a normal data step instead?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input (D_EFFET D_VALO) (:datetime.) NO_POLICE :$20. MT_BRUT_CIE cumul_vers nombre date :ddmmyy. ;
  format D_EFFET D_VALO datetime19. date yymmdd10. ;
cards;
20APR2010:00:00:00 29APR2010:09:02:02 I99000001 -40000 1013137.23 3 02/02/2018
21FEB2013:00:00:00 27FEB2013:09:29:19 I99000001 -873137.23 1013137.23 3 02/02/2018
02FEB2018:00:00:00 20FEB2018:12:02:35 I99000001 -100000 1013137.23 3 02/02/2018
;

proc sort data=have;
  by no_police D_EFFET ;
run;

data want;
  set have;
  by no_police D_EFFET ;
  if first.no_police then do;
    running_total=0;
    d_effet_count=0;
  end;
  d_effet_count+first.d_effet;
  if not last.no_police then running_total + abs(mt_brut_cie);
run;

proc print;
  var no_police d_effet d_effet_count running_total mt_brut_cie ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is this what you are expecting?&lt;/P&gt;
&lt;PRE&gt;                                           d_effet_     running_     MT_BRUT_
Obs    NO_POLICE                D_EFFET      count       total          CIE

 1     I99000001     20APR2010:00:00:00        1        40000.00     -40000.00
 2     I99000001     21FEB2013:00:00:00        2       913137.23    -873137.23
 3     I99000001     02FEB2018:00:00:00        3       913137.23    -100000.00

&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 04 Sep 2021 22:18:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766058#M242728</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-04T22:18:35Z</dc:date>
    </item>
    <item>
      <title>Re: First, last and where clauses in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766098#M242746</link>
      <description>&lt;P&gt;Thank you, Tom,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My code in 2 steps could be&amp;nbsp; :&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
	create table test as select a.*,
		sum(abs(a.MT_BRUT_CIE))as cumul_vers, count(distinct(a.D_EFFET)) as nombre,
		max(datepart(a.D_EFFET)) as date format ddmmyy10.
       /*cumul_vers-(abs(a.MT_BRUT_CIE) where date=datepart(a.D_EFFET)) and nombre&amp;gt;1*/
      from PRESTA as a
	group by NO_POLICE;
quit;

data PRESTA2;
	set test;
    by NO_POLICE;
	if date=datepart(D_EFFET) and nombre&amp;gt;1 then
		do;
		    retain;
			montant=cumul_vers-(abs(MT_BRUT_CIE));
		end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The result is like :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MarieT_0-1630833506727.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/63306iE7F67A8C39441B65/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MarieT_0-1630833506727.png" alt="MarieT_0-1630833506727.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm wondering if I can rewrite it in proc sql sas, I found on Internet this answers&amp;nbsp; :&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MarieT_1-1630833589480.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/63307i41601F937563473A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MarieT_1-1630833589480.png" alt="MarieT_1-1630833589480.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MarieT_2-1630833614452.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/63308iB340DECD15AA9E31/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MarieT_2-1630833614452.png" alt="MarieT_2-1630833614452.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I run this code I have error on top(1)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to use the proc sql, because after I rewrite this code on sql adodb in VBA, so it more easy for me to start with the proc sql.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much for your help !&lt;/P&gt;</description>
      <pubDate>Sun, 05 Sep 2021 09:24:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766098#M242746</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2021-09-05T09:24:15Z</dc:date>
    </item>
    <item>
      <title>Re: First, last and where clauses in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766101#M242748</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if date=datepart(D_EFFET) and nombre&amp;gt;1 then
		do;
		    retain;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;RETAIN is a declarative statement to declare variables as retained, which means they are not reset to missing at the start of a data step iteration.&lt;/P&gt;
&lt;P&gt;It cannot be executed conditionally, and without a variable name it has no effect at all.&lt;/P&gt;
&lt;P&gt;What do you want to retain between data step iterations?&lt;/P&gt;</description>
      <pubDate>Sun, 05 Sep 2021 11:49:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766101#M242748</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-09-05T11:49:06Z</dc:date>
    </item>
    <item>
      <title>Re: First, last and where clauses in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766104#M242749</link>
      <description>Thank you, for your answear, Kurt.&lt;BR /&gt;It was just the example of the result that I need to get.&lt;BR /&gt;&lt;BR /&gt;I would like to rewrite this 2 steps code on 1 step on proc sql , but I don't know how to do it. &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;</description>
      <pubDate>Sun, 05 Sep 2021 12:01:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766104#M242749</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2021-09-05T12:01:33Z</dc:date>
    </item>
    <item>
      <title>Re: First, last and where clauses in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766116#M242752</link>
      <description>&lt;P&gt;Please explain in words what your 2 step code is doing.&lt;/P&gt;
&lt;P&gt;Where is NOMBRE coming from?&amp;nbsp; What does NOMBRE&amp;gt;1 mean in this case?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The SQL language as originally defined in the 1980's and codified into 1992 standard that PROC SQL supports has no concept of first and last. Other implementations of SQL added extra non-standard features to get around this and ultimately the SQL standard was expanded to at least include windowing functions that allow something like processing records in a ordered manner.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you don't need them in SAS because SAS has a full blown data processing language of its own called the data step.&lt;/P&gt;</description>
      <pubDate>Sun, 05 Sep 2021 15:12:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766116#M242752</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-05T15:12:14Z</dc:date>
    </item>
    <item>
      <title>Re: First, last and where clauses in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766118#M242753</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Explain in words what you are trying to calculate.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;It looks a lot like a running total, but that is not clear at all, and your example does not have enough variation to detect any pattern into what you are trying to do.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It looks like there are only three important variables&amp;nbsp;NO_POLICE,&amp;nbsp;D_EFFET and&amp;nbsp;MT_BRUT_CIE.&lt;/P&gt;
&lt;P&gt;You are grouping by NO_POLICE.&lt;/P&gt;
&lt;P&gt;You are taking the SUM() of&amp;nbsp;MT_BRUT_CIE (please explain why the ABS() function is needed).&lt;/P&gt;
&lt;P&gt;You are counting distinct values of the datetime value of&amp;nbsp;D_EFFET (are there really multiple records for the exact same point in time?).&lt;/P&gt;
&lt;P&gt;You are converting D_EFFET from a datetime into a date.&amp;nbsp; Are there time of day values that will be lost? What happens if there are more than one distinct D_EFFET values on the same day for the same NO_POLICE value?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why are then sometimes subtracting the value of&amp;nbsp;MT_BRUT_CIE back out of the SUM()?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 05 Sep 2021 15:22:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766118#M242753</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-05T15:22:21Z</dc:date>
    </item>
    <item>
      <title>Re: First, last and where clauses in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766121#M242754</link>
      <description>I would like to summuriez the MT_BRUT_CIE by NO_POLICE.&lt;BR /&gt;If  the D_EFFET (by NO_POLICE) is different I need to substract the most recent  MT_BRUT_CIE from the sum.</description>
      <pubDate>Sun, 05 Sep 2021 15:36:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766121#M242754</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2021-09-05T15:36:46Z</dc:date>
    </item>
    <item>
      <title>Re: First, last and where clauses in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766122#M242755</link>
      <description>&lt;P&gt;I improve my code, but I don't know how to manage the case with the dates are same :&lt;/P&gt;
&lt;P&gt;I added updated data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much for your help !&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

proc sql;
	create table test as select distinct count(distinct(a.D_EFFET)) as nombre,a.NO_POLICE, a.MT_BRUT_CIE,
		sum(abs(a.MT_BRUT_CIE)) as montant,sum(abs(a.MT_BRUT_CIE))-abs(a.MT_BRUT_CIE) as montant2, max(datepart(a.D_EFFET)) as date_recente format ddmmyy10., datepart(a.D_EFFET) as date format ddmmyy10.
	from PRESTATIONS2 as a
 having  max(datepart(a.D_EFFET))=datepart(a.D_EFFET)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 05 Sep 2021 15:45:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766122#M242755</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2021-09-05T15:45:01Z</dc:date>
    </item>
    <item>
      <title>Re: First, last and where clauses in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766123#M242756</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/286185"&gt;@SASdevAnneMarie&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I would like to summuriez the MT_BRUT_CIE by NO_POLICE.&lt;BR /&gt;If the D_EFFET (by NO_POLICE) is different I need to substract the most recent MT_BRUT_CIE from the sum.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;But what is the &lt;STRONG&gt;meaning&lt;/STRONG&gt; of doing that final subtraction?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sounds like you are saying if there is only one date you want the total, but if there is two more dates you want the total without the last date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What happens if there are multiple records on the last date?&amp;nbsp; Do you want to subtract all of the values?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you just want the collapsed total per NO_POLICE?&amp;nbsp; Or do you want the total re-merged back onto all of the detailed records?&lt;/P&gt;
&lt;P&gt;Do you want the modified total re-merged onto all of the detail records?&amp;nbsp; Or do you just want the value of that total variable to be different on the last observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do the D_EFFET variables ever have time of day part? Or are they really just a date value that happens to be stored as datetime values?&amp;nbsp; Perhaps you pulled it from a database like ORACLE or EXCEL that does not really have Date or Time types, just DATETIME?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What would be results of for this data?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;GROUP  DATE AMOUNT
1 2021-01-01 100
1 2021-01-02 200
1 2021-01-03 300
2 2021-01-01 100
3 2021-01-01 100
3 2021-01-02 200
3 2021-01-02 300&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 05 Sep 2021 15:46:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766123#M242756</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-05T15:46:42Z</dc:date>
    </item>
    <item>
      <title>Re: First, last and where clauses in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766127#M242757</link>
      <description>&lt;P&gt;But what is the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;meaning&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;of doing that final subtraction?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sounds like you are saying if there is only one date you want the total, but if there is two more dates you want the total without the last date.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;- I want the total without the observation from last day.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What happens if there are multiple records on the last date?&amp;nbsp; Do you want to subtract all of the values?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;-Yes, all the values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you just want the collapsed total per NO_POLICE?&amp;nbsp; Or do you want the total re-merged back onto all of the detailed records?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;- Total collapsed without remerging.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you want the modified total re-merged onto all of the detail records?&amp;nbsp; Or do you just want the value of that total variable to be different on the last observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;- I need this result :&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MarieT_0-1630857334286.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/63314i35346D42946C1A63/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MarieT_0-1630857334286.png" alt="MarieT_0-1630857334286.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My code is correct, but I can't hold the case when I have, fro example, 3 observation with same DATES, I have this :&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MarieT_1-1630857473495.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/63315i7B0059010E0D1F91/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MarieT_1-1630857473495.png" alt="MarieT_1-1630857473495.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to have&amp;nbsp;47506,40 in montant2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do the D_EFFET variables ever have time of day part? Or are they really just a date value that happens to be stored as datetime values?&amp;nbsp; Perhaps you pulled it from a database like ORACLE or EXCEL that does not really have Date or Time types, just DATETIME? Sorry, I didn't understant the question.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My actual code is :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
	create table test as select distinct count(distinct(a.D_EFFET)) as nombre,a.NO_POLICE, a.MT_BRUT_CIE,
		sum(abs(a.MT_BRUT_CIE)) as montant,sum(abs(a.MT_BRUT_CIE))-abs(a.MT_BRUT_CIE) as montant2, max(datepart(a.D_EFFET)) as date_recente format ddmmyy10., datepart(a.D_EFFET) as date format ddmmyy10.
	from PRESTATIONS2 as a
 having  max(datepart(a.D_EFFET))=datepart(a.D_EFFET)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thank you !&lt;/P&gt;</description>
      <pubDate>Sun, 05 Sep 2021 15:59:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766127#M242757</guid>
      <dc:creator>SASdevAnneMarie</dc:creator>
      <dc:date>2021-09-05T15:59:33Z</dc:date>
    </item>
    <item>
      <title>Re: First, last and where clauses in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766129#M242759</link>
      <description>&lt;P&gt;So if this is only one day you want the total to be zero?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 05 Sep 2021 16:52:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766129#M242759</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-05T16:52:32Z</dc:date>
    </item>
    <item>
      <title>Re: First, last and where clauses in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766130#M242760</link>
      <description>&lt;P&gt;It really helps to build test data that exercises the boundary conditions of your logic.&lt;/P&gt;
&lt;P&gt;Here is data for three groups.&amp;nbsp; One with multiple days and multiple observations on the last day.&amp;nbsp; Another with multiple days and a single observations on the last day.&amp;nbsp; One with just a single observation.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input no_police :$20. d_effet :datetime. mt_brut_cie ;
  format d_effet datetime19.;
cards;
1 01JAN2020:00:00 100
1 02JAN2020:00:00 100
1 03JAN2020:00:00 100
1 03JAN2020:00:00 100
1 03JAN2020:00:00 100
2 01JAN2020:00:00 100
2 02JAN2020:00:00 100
2 03JAN2020:00:00 100
3 03JAN2020:00:00 100
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If I understand your rules then you want this output for those three cases:&lt;/P&gt;
&lt;PRE&gt;Obs    no_police    total

 1         1         200
 2         2         200
 3         3           0
&lt;/PRE&gt;
&lt;P&gt;Here is code that gets that result that only using a single SQL statement (a complex one, but just one).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
  select no_police
       , min(grand_total - case when (date=lastdate) then day_total else 0 end) as total
  from
    (
      select *
           , sum(day_total) as grand_total
           , max(date) as lastdate format=yymmdd10.
      from
      (
        select no_police,datepart(d_effet) as date format=yymmdd10. 
             , sum(abs(mt_brut_cie)) as day_total
          from have
          group by no_police,date
      )
      group by no_police
    )
  group by no_police
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Working from the inside out:&lt;BR /&gt;Calculate the total per group per day.&lt;/P&gt;
&lt;P&gt;Merge back on the grand total and the date of the last day per group.&lt;/P&gt;
&lt;P&gt;Finally calculate the results of subtracting the last day's total from the grand total.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 05 Sep 2021 17:04:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-last-and-where-clauses-in-proc-sql/m-p/766130#M242760</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-05T17:04:38Z</dc:date>
    </item>
  </channel>
</rss>

