<?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 Finding the maximum value of each unique ID while keeping all other columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Finding-the-maximum-value-of-each-unique-ID-while-keeping-all/m-p/374227#M89580</link>
    <description>&lt;P&gt;Hi, I am using SAS to try to calculate the maximum value of a variable for each unique person. However, I want to retain the other columns of the dataset for the row that contains that maximum value. I've tried to use PROC SQL to accomplish this, but I have no idea how to make sure that the other columns also transfer over. For example, my dataset has 250,000 rows, but only 32,000 unique individuals. I want to find the maximum date for each unique person, and for the row that contains the maximum, I also want to retain all other columns. This is my code so far:&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 ID, max(date_var) from dataset
    group by 1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Any help would be great. Thanks.&lt;/P&gt;</description>
    <pubDate>Sun, 09 Jul 2017 06:11:13 GMT</pubDate>
    <dc:creator>corkee</dc:creator>
    <dc:date>2017-07-09T06:11:13Z</dc:date>
    <item>
      <title>Finding the maximum value of each unique ID while keeping all other columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-the-maximum-value-of-each-unique-ID-while-keeping-all/m-p/374227#M89580</link>
      <description>&lt;P&gt;Hi, I am using SAS to try to calculate the maximum value of a variable for each unique person. However, I want to retain the other columns of the dataset for the row that contains that maximum value. I've tried to use PROC SQL to accomplish this, but I have no idea how to make sure that the other columns also transfer over. For example, my dataset has 250,000 rows, but only 32,000 unique individuals. I want to find the maximum date for each unique person, and for the row that contains the maximum, I also want to retain all other columns. This is my code so far:&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 ID, max(date_var) from dataset
    group by 1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Any help would be great. Thanks.&lt;/P&gt;</description>
      <pubDate>Sun, 09 Jul 2017 06:11:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-the-maximum-value-of-each-unique-ID-while-keeping-all/m-p/374227#M89580</guid>
      <dc:creator>corkee</dc:creator>
      <dc:date>2017-07-09T06:11:13Z</dc:date>
    </item>
    <item>
      <title>Re: Finding the maximum value of each unique ID while keeping all other columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-the-maximum-value-of-each-unique-ID-while-keeping-all/m-p/374232#M89583</link>
      <description>&lt;P&gt;Proc sort and a data step will work easily as well.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc sort data=have;
By Id variable;
Run;

Data want;
Set have;
By Id;
If last.id;
Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;However, if there's the possibility that there may be duplicates of the value SQL is easier. You can use the HAVING clause to keep only the rows of interest.&amp;nbsp;&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 *, max(variable) as max_value
From have
Group by Id 
Having max(variable)=variable;
Quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/122635"&gt;@corkee&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi, I am using SAS to try to calculate the maximum value of a variable for each unique person. However, I want to retain the other columns of the dataset for the row that contains that maximum value. I've tried to use PROC SQL to accomplish this, but I have no idea how to make sure that the other columns also transfer over. For example, my dataset has 250,000 rows, but only 32,000 unique individuals. I want to find the maximum date for each unique person, and for the row that contains the maximum, I also want to retain all other columns. This is my code so far:&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 ID, max(date_var) from dataset
    group by 1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Any help would be great. Thanks.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 09 Jul 2017 07:38:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-the-maximum-value-of-each-unique-ID-while-keeping-all/m-p/374232#M89583</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-07-09T07:38:48Z</dc:date>
    </item>
    <item>
      <title>Re: Finding the maximum value of each unique ID while keeping all other columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-the-maximum-value-of-each-unique-ID-while-keeping-all/m-p/374234#M89585</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/122635"&gt;@corkee&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;The &lt;EM&gt;Proc Sort / Data Step&lt;/EM&gt; option&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;posted will likely perform better than the SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Should it be possible that you have more than one record with a max date_var in an ID then the &lt;EM&gt;SQL&lt;/EM&gt; will return all rows with a max value but the &lt;EM&gt;Proc Sort / Data Step&lt;/EM&gt; will only return one row. Your pick!&lt;/P&gt;</description>
      <pubDate>Sun, 09 Jul 2017 08:00:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-the-maximum-value-of-each-unique-ID-while-keeping-all/m-p/374234#M89585</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-07-09T08:00:54Z</dc:date>
    </item>
  </channel>
</rss>

