<?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: sum over multiple records and retain other variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sum-over-multiple-records-and-retain-other-variables/m-p/946073#M370554</link>
    <description>&lt;P&gt;Here's how to build and example data set and then summarize as requested for this data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
input ID AMOUNT year ;
datalines;
8888 50 110
8889 1.10 110
8889 2.99 111
8890 3.48 110
8891 34.08 110
8891 144.87 110
8891 2.24 111
8892 0 110
;

proc summary data=have nway;
   class id;
   var amount year;
   output out=want (drop=_:) sum(amount)=amount max(year)=year
   ;
run;&lt;/PRE&gt;
&lt;P&gt;That assumes you want a data set.&lt;/P&gt;
&lt;P&gt;A report could be:&lt;/P&gt;
&lt;PRE&gt;Proc report data=have;
   columns id amount year;
   define id / group;
   define amount/ sum;
   define year /max;
run;&lt;/PRE&gt;
&lt;P&gt;The options in Proc summary : Nway only the highest combination of types. Default is that summary will create multiple summaries combining different levels of Class variables.&lt;/P&gt;
&lt;P&gt;The Drop= on the Output statement says to drop variables that start with _, which would be _type_ and _freq_.&lt;/P&gt;
&lt;P&gt;Max with something like Year, the largest value, would likely be the "most recent" or "latest" value. If you had wanted the first that could be MIN. Other desires might require more complicated programming.&lt;/P&gt;</description>
    <pubDate>Thu, 03 Oct 2024 04:32:25 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2024-10-03T04:32:25Z</dc:date>
    <item>
      <title>sum over multiple records and retain other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-over-multiple-records-and-retain-other-variables/m-p/946068#M370553</link>
      <description>&lt;P&gt;I have a data have 3 avariles:&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID AMOUNT year&lt;BR /&gt;8888 50 110&lt;BR /&gt;8889 1.10 110&lt;BR /&gt;8889 2.99 111&lt;BR /&gt;8890 3.48 110&lt;BR /&gt;8891 34.08 110&lt;BR /&gt;8891 144.87 110&lt;BR /&gt;8891 2.24 111&lt;BR /&gt;8892 0 110&lt;BR /&gt;&lt;BR /&gt;how to sum over multiple records for amont and keep ID and most recnt year if the obervation has multiple records in SAS?&lt;/P&gt;&lt;P&gt;8888 50 110&lt;BR /&gt;8889 4.09 111&lt;BR /&gt;8890 3.48 110&lt;BR /&gt;8891 181.19 111&lt;BR /&gt;8892 0 110&lt;/P&gt;</description>
      <pubDate>Thu, 03 Oct 2024 03:30:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-over-multiple-records-and-retain-other-variables/m-p/946068#M370553</guid>
      <dc:creator>tinghlin</dc:creator>
      <dc:date>2024-10-03T03:30:33Z</dc:date>
    </item>
    <item>
      <title>Re: sum over multiple records and retain other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-over-multiple-records-and-retain-other-variables/m-p/946073#M370554</link>
      <description>&lt;P&gt;Here's how to build and example data set and then summarize as requested for this data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
input ID AMOUNT year ;
datalines;
8888 50 110
8889 1.10 110
8889 2.99 111
8890 3.48 110
8891 34.08 110
8891 144.87 110
8891 2.24 111
8892 0 110
;

proc summary data=have nway;
   class id;
   var amount year;
   output out=want (drop=_:) sum(amount)=amount max(year)=year
   ;
run;&lt;/PRE&gt;
&lt;P&gt;That assumes you want a data set.&lt;/P&gt;
&lt;P&gt;A report could be:&lt;/P&gt;
&lt;PRE&gt;Proc report data=have;
   columns id amount year;
   define id / group;
   define amount/ sum;
   define year /max;
run;&lt;/PRE&gt;
&lt;P&gt;The options in Proc summary : Nway only the highest combination of types. Default is that summary will create multiple summaries combining different levels of Class variables.&lt;/P&gt;
&lt;P&gt;The Drop= on the Output statement says to drop variables that start with _, which would be _type_ and _freq_.&lt;/P&gt;
&lt;P&gt;Max with something like Year, the largest value, would likely be the "most recent" or "latest" value. If you had wanted the first that could be MIN. Other desires might require more complicated programming.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Oct 2024 04:32:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-over-multiple-records-and-retain-other-variables/m-p/946073#M370554</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-10-03T04:32:25Z</dc:date>
    </item>
    <item>
      <title>Re: sum over multiple records and retain other variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sum-over-multiple-records-and-retain-other-variables/m-p/946125#M370557</link>
      <description>&lt;P&gt;Here's a Data Step Approach using Group Processing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data table;&lt;BR /&gt;input ID AMOUNT year;&lt;BR /&gt;datalines;&lt;BR /&gt;8888 50 110&lt;BR /&gt;8889 1.10 110&lt;BR /&gt;8889 2.99 111&lt;BR /&gt;8890 3.48 110&lt;BR /&gt;8891 34.08 110&lt;BR /&gt;8891 144.87 110&lt;BR /&gt;8891 2.24 111&lt;BR /&gt;8892 0 110&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/* A prerequisite sort is needed in order to use Group Processing in the Data Step */&lt;BR /&gt;proc sort data=table out=table_s;&lt;BR /&gt;by id year;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data table_sum;&lt;BR /&gt;set table_s;&lt;BR /&gt;by id year; /* This statement creates hidden variables in the PDV that &lt;BR /&gt;are used to control groups in this case&lt;BR /&gt;first.id, last.id, first.year, last.year */&lt;BR /&gt;if first.id then&lt;BR /&gt;total=0;&lt;BR /&gt;Total + amount;&lt;/P&gt;
&lt;P&gt;if last.id and last.year;&lt;BR /&gt;drop amount;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Oct 2024 14:43:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sum-over-multiple-records-and-retain-other-variables/m-p/946125#M370557</guid>
      <dc:creator>JOL</dc:creator>
      <dc:date>2024-10-03T14:43:40Z</dc:date>
    </item>
  </channel>
</rss>

