<?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 to keep only the last observation of a summed group? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-only-the-last-observation-of-a-summed-group/m-p/586896#M167546</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285214"&gt;@JDDowell&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;1. It is not clear from you code/description whether you want to sum within ProgramIndex, MonthOfService, or the latter within the former. Please show your suggested output data set related to your sample input.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. Your addend N_Youth must be numeric, yet you input it as character. Try this input instead:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data PI ;                                                 
  input MonthOfService :MonYY5. N_Youth ProgramIndex :$5 ;
  format MonthofService MonYY5. ;                         
  cards ;                                                 
JUL13    6  A2111                                         
JUL13    2  A2111                                         
JUL13   20  A2111                                         
JUL13  155  A2106                                         
JUL13   10  A2106                                         
JUL13   44  A2109                                         
JUL13    7  A2124                                         
AUG13    7  A2111                                         
AUG13    2  A2111                                         
AUG13   23  A2111                                         
AUG13  167  A2106                                         
AUG13   14  A2106                                         
AUG13   46  A2109                                         
AUG13    7  A2124                                         
AUG13    5  A2124                                         
;                                                         
run ;                                                     
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;3. You cannot use last.MonthOfService if MonthOfService is not listed in the BY statement - and the input data set should be sorted accordingly.&lt;/P&gt;
&lt;P&gt;4. You're missing a semicolon in the related subsetting IF statement.&lt;/P&gt;
&lt;P&gt;5. I'd suggest you do it using proc MEANS/SUMMARY or SQL instead of the DATA step. But to show you how, one needs to have a clear picture of what you want to achieve. Go to #1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
    <pubDate>Fri, 06 Sep 2019 22:16:28 GMT</pubDate>
    <dc:creator>hashman</dc:creator>
    <dc:date>2019-09-06T22:16:28Z</dc:date>
    <item>
      <title>How to keep only the last observation of a summed group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-only-the-last-observation-of-a-summed-group/m-p/586891#M167543</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data PI;
	Infile datalines dlm=',';
	input MonthOfService MonYY5. N_Youth $ ProgramIndex $;
	format MonthofService MonYY5.;
	datalines; 
JUL13,	6,	A2111
JUL13,	2,	A2111
JUL13,	20,	A2111
JUL13,	155,	A2106
JUL13,	10,	A2106
JUL13,	44,	A2109
JUL13,	7,	A2124
AUG13,	7,	A2111
AUG13,	2,	A2111
AUG13,	23,	A2111
AUG13,	167,	A2106
AUG13,	14,	A2106
AUG13,	46,	A2109
AUG13,	7,	A2124
AUG13,	5,	A2124

proc sort data=PI;
by ProgramIndex;
run;

data PI_numbers;
set PI;
By ProgramIndex;
if First.ProgramIndex then Num_Youth=0;
Num_Youth + N_Youth;&lt;BR /&gt;If Last.MonthOfService
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I'm trying to keep only the last observation after I have summed up N_Youth for the ProgramIndex. I can get the sum for each program index but I also get all previous sums value as well. I try to use&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Last.MonthOfService&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;to only record the last value of a given month, but when I do my table contains no values. Is it the date format?&lt;/P&gt;</description>
      <pubDate>Fri, 06 Sep 2019 21:58:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-only-the-last-observation-of-a-summed-group/m-p/586891#M167543</guid>
      <dc:creator>JDDowell</dc:creator>
      <dc:date>2019-09-06T21:58:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep only the last observation of a summed group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-only-the-last-observation-of-a-summed-group/m-p/586895#M167545</link>
      <description>Replace&lt;BR /&gt;&lt;BR /&gt;If last.MonthOfService;&lt;BR /&gt;&lt;BR /&gt;Instead, use&lt;BR /&gt;&lt;BR /&gt;If last.ProgramIndex;</description>
      <pubDate>Fri, 06 Sep 2019 22:14:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-only-the-last-observation-of-a-summed-group/m-p/586895#M167545</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-09-06T22:14:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep only the last observation of a summed group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-only-the-last-observation-of-a-summed-group/m-p/586896#M167546</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285214"&gt;@JDDowell&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;1. It is not clear from you code/description whether you want to sum within ProgramIndex, MonthOfService, or the latter within the former. Please show your suggested output data set related to your sample input.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. Your addend N_Youth must be numeric, yet you input it as character. Try this input instead:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data PI ;                                                 
  input MonthOfService :MonYY5. N_Youth ProgramIndex :$5 ;
  format MonthofService MonYY5. ;                         
  cards ;                                                 
JUL13    6  A2111                                         
JUL13    2  A2111                                         
JUL13   20  A2111                                         
JUL13  155  A2106                                         
JUL13   10  A2106                                         
JUL13   44  A2109                                         
JUL13    7  A2124                                         
AUG13    7  A2111                                         
AUG13    2  A2111                                         
AUG13   23  A2111                                         
AUG13  167  A2106                                         
AUG13   14  A2106                                         
AUG13   46  A2109                                         
AUG13    7  A2124                                         
AUG13    5  A2124                                         
;                                                         
run ;                                                     
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;3. You cannot use last.MonthOfService if MonthOfService is not listed in the BY statement - and the input data set should be sorted accordingly.&lt;/P&gt;
&lt;P&gt;4. You're missing a semicolon in the related subsetting IF statement.&lt;/P&gt;
&lt;P&gt;5. I'd suggest you do it using proc MEANS/SUMMARY or SQL instead of the DATA step. But to show you how, one needs to have a clear picture of what you want to achieve. Go to #1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Sep 2019 22:16:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-only-the-last-observation-of-a-summed-group/m-p/586896#M167546</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-09-06T22:16:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to keep only the last observation of a summed group?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-keep-only-the-last-observation-of-a-summed-group/m-p/586933#M167565</link>
      <description>&lt;P&gt;I'm only guessing, but I think you mean to do this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data PI ;                                                 
  input MonthOfService :MonYY5. N_Youth ProgramIndex :$5. ;
  format MonthofService MonYY5. ;                         
  cards ;                                                 
JUL13    6  A2111                                         
JUL13    2  A2111                                         
JUL13   20  A2111                                         
JUL13  155  A2106                                         
JUL13   10  A2106                                         
JUL13   44  A2109                                         
JUL13    7  A2124                                         
AUG13    7  A2111                                         
AUG13    2  A2111                                         
AUG13   23  A2111                                         
AUG13  167  A2106                                         
AUG13   14  A2106                                         
AUG13   46  A2109                                         
AUG13    7  A2124                                         
AUG13    5  A2124                                         
;

data want;
do until (last.programIndex);
    set PI; by MonthOfService ProgramIndex notsorted;
    num_youth = sum(num_youth, N_Youth);
    end;
keep MonthOfService ProgramIndex num_youth;
run;

proc print data=want noobs; run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                             Month
                              Of       Program     num_
                            Service     Index     youth

                             JUL13      A2111       28
                             JUL13      A2106      165
                             JUL13      A2109       44
                             JUL13      A2124        7
                             AUG13      A2111       32
                             AUG13      A2106      181
                             AUG13      A2109       46
                             AUG13      A2124       12
&lt;/PRE&gt;</description>
      <pubDate>Sat, 07 Sep 2019 03:46:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-keep-only-the-last-observation-of-a-summed-group/m-p/586933#M167565</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-09-07T03:46:55Z</dc:date>
    </item>
  </channel>
</rss>

