<?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 Getting new record with SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494683#M130405</link>
    <description>&lt;P&gt;Hi! I have this dataset with old and new birth dates for each name. I'd like to get the birth date from the rows with Source = "NEW". But for names with no Source = "NEW" (as in the case below SARAH), I'd like to retain the old record.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'd like to do it with only one SQL statement without need of joining tables, I'm thinking of a group by and min/ max statement.. but I can't get it right. Or is it not possible? Thank you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;/P&gt;
&lt;P&gt;infile datalines missover;&lt;BR /&gt;input NAME :$5. BIRTHDATE :$8. SOURCE :$3. ADDRESS :$8.;&lt;BR /&gt;datalines;&lt;BR /&gt;ANGEL 09122018 OLD USA&lt;BR /&gt;ANGEL 09112018 NEW &lt;BR /&gt;FELIX 07252017 OLD UK&lt;BR /&gt;FELIX 07252017 NEW&lt;BR /&gt;SARAH 09092018 OLD CANADA&lt;BR /&gt;;&lt;BR /&gt;run;&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;data want;&lt;/P&gt;
&lt;P&gt;infile datalines missover;&lt;BR /&gt;input NAME :$5. BIRTHDATE :$8. SOURCE :$3. ADDRESS :$8.;&lt;BR /&gt;datalines;&lt;BR /&gt;ANGEL 09112018 NEW USA&lt;BR /&gt;FELIX 07252017 NEW UK&lt;BR /&gt;SARAH 09092018 OLD CANADA&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
    <pubDate>Wed, 12 Sep 2018 01:37:57 GMT</pubDate>
    <dc:creator>angeliquec</dc:creator>
    <dc:date>2018-09-12T01:37:57Z</dc:date>
    <item>
      <title>Getting new record with SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494683#M130405</link>
      <description>&lt;P&gt;Hi! I have this dataset with old and new birth dates for each name. I'd like to get the birth date from the rows with Source = "NEW". But for names with no Source = "NEW" (as in the case below SARAH), I'd like to retain the old record.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'd like to do it with only one SQL statement without need of joining tables, I'm thinking of a group by and min/ max statement.. but I can't get it right. Or is it not possible? Thank you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;/P&gt;
&lt;P&gt;infile datalines missover;&lt;BR /&gt;input NAME :$5. BIRTHDATE :$8. SOURCE :$3. ADDRESS :$8.;&lt;BR /&gt;datalines;&lt;BR /&gt;ANGEL 09122018 OLD USA&lt;BR /&gt;ANGEL 09112018 NEW &lt;BR /&gt;FELIX 07252017 OLD UK&lt;BR /&gt;FELIX 07252017 NEW&lt;BR /&gt;SARAH 09092018 OLD CANADA&lt;BR /&gt;;&lt;BR /&gt;run;&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;data want;&lt;/P&gt;
&lt;P&gt;infile datalines missover;&lt;BR /&gt;input NAME :$5. BIRTHDATE :$8. SOURCE :$3. ADDRESS :$8.;&lt;BR /&gt;datalines;&lt;BR /&gt;ANGEL 09112018 NEW USA&lt;BR /&gt;FELIX 07252017 NEW UK&lt;BR /&gt;SARAH 09092018 OLD CANADA&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Sep 2018 01:37:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494683#M130405</guid>
      <dc:creator>angeliquec</dc:creator>
      <dc:date>2018-09-12T01:37:57Z</dc:date>
    </item>
    <item>
      <title>Re: Getting new record with SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494687#M130406</link>
      <description>&lt;P&gt;If NEW is always the last record for the person, keep that last record.&lt;/P&gt;
&lt;PRE&gt;data WANT;
&amp;nbsp;set HAVE;
&amp;nbsp;by NAME ;
&amp;nbsp;if last.NAME;
run;&lt;/PRE&gt;
&lt;P&gt;If not, sort and apply that same logic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Sep 2018 01:34:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494687#M130406</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-09-12T01:34:33Z</dc:date>
    </item>
    <item>
      <title>Re: Getting new record with SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494691#M130408</link>
      <description>&lt;P&gt;SAS BY grouping is really powerful in these situations. You can easily do it in SAS, in SQL it can be done but it's not as easy. I would do, two passes, one with the new only and then for records that have a single record, take that.&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 new records*/
select *
from have
where source='NEW'
union
/*add in records with a single entry and source='OLD', single NEW would already be included if possible*/
select *
from have
group by name
having count(name)=1 and source='OLD';
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Sep 2018 02:18:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494691#M130408</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-09-12T02:18:39Z</dc:date>
    </item>
    <item>
      <title>Re: Getting new record with SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494693#M130410</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;

infile datalines missover;
input NAME :$5. BIRTHDATE :$8. SOURCE :$3. ADDRESS :$8.;
datalines;
ANGEL 09122018 OLD USA
ANGEL 09112018 NEW 
FELIX 07252017 OLD UK
FELIX 07252017 NEW
SARAH 09092018 OLD CANADA
;
run;


proc sql;
create table want(drop=s) as
select *, SOURCE='NEW' as s
from have
group by name
having max(S)=s ;
quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Sep 2018 02:31:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494693#M130410</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-09-12T02:31:43Z</dc:date>
    </item>
    <item>
      <title>Re: Getting new record with SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494703#M130416</link>
      <description>&lt;P&gt;Clever! &lt;img id="smileywink" class="emoticon emoticon-smileywink" src="https://communities.sas.com/i/smilies/16x16_smiley-wink.png" alt="Smiley Wink" title="Smiley Wink" /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Sep 2018 03:37:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494703#M130416</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-09-12T03:37:35Z</dc:date>
    </item>
    <item>
      <title>Re: Getting new record with SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494704#M130417</link>
      <description>&lt;P&gt;I did it slightly different, since the other&amp;nbsp;solutions didn't seem to resolve keeping the address. I still think a solution using a data step with a by statement is probably better.&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 distinct 
	t1.name
	,case when t2.name ne '' then t2.birthdate else t1.birthdate end as birthdate
	,case when t2.name ne '' then t2.source else t1.source end as source
	,t1.address
from have as t1
left join (select * from have where source = 'NEW') as t2
on t1.name=t2.name
where t1.address ne ''
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Sep 2018 03:43:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494704#M130417</guid>
      <dc:creator>bobpep212</dc:creator>
      <dc:date>2018-09-12T03:43:06Z</dc:date>
    </item>
    <item>
      <title>Re: Getting new record with SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494705#M130418</link>
      <description>&lt;P&gt;Thank you sir&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;One day I want to code like you. That's exactly the same I told my parents. i was advised by them to copy you shamelessly. Have a great night!&lt;/P&gt;</description>
      <pubDate>Wed, 12 Sep 2018 03:49:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494705#M130418</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-09-12T03:49:09Z</dc:date>
    </item>
    <item>
      <title>Re: Getting new record with SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494706#M130419</link>
      <description>&lt;P&gt;Note that &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;'s query can be extended to keep the address when there is one, like this:&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(drop=s) as
select 
    NAME,
    Birthdate, 
    Source,
    coalesce(address, max(address)) as address,
    SOURCE='NEW' as s
from have
group by name
having max(S)=s ;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Sep 2018 03:55:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494706#M130419</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-09-12T03:55:06Z</dc:date>
    </item>
    <item>
      <title>Re: Getting new record with SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494707#M130420</link>
      <description>Ah YES! COALESCE!</description>
      <pubDate>Wed, 12 Sep 2018 03:57:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494707#M130420</guid>
      <dc:creator>bobpep212</dc:creator>
      <dc:date>2018-09-12T03:57:56Z</dc:date>
    </item>
    <item>
      <title>Re: Getting new record with SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494708#M130421</link>
      <description>&lt;P&gt;This works too ?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql;
create table want(drop=s) as
select NAME,
    Birthdate, 
    Source,
     max(address) as address,
    SOURCE='NEW' as s
from have
group by name
having max(S)=s ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Sep 2018 04:00:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494708#M130421</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-09-12T04:00:22Z</dc:date>
    </item>
    <item>
      <title>Re: Getting new record with SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494709#M130422</link>
      <description>&lt;P&gt;Sir, no need for coalesce coz implicit remerge will take care of it anyway. I was a little tipsy to not pay attention to it in the 1st place. Thanks as always and again&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(drop=s) as
select 
    NAME,
    Birthdate, 
    Source,
    max(address) as address,
    SOURCE='NEW' as s
from have
group by name
having max(S)=s ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Sep 2018 04:02:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494709#M130422</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-09-12T04:02:47Z</dc:date>
    </item>
    <item>
      <title>Re: Getting new record with SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494710#M130423</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;, well, I thought that if there was an address in a NEW record, I would want that one, even if the address in the OLD record was greater (lexicographically).&lt;/P&gt;</description>
      <pubDate>Wed, 12 Sep 2018 04:08:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494710#M130423</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-09-12T04:08:22Z</dc:date>
    </item>
    <item>
      <title>Re: Getting new record with SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494711#M130424</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp; Good thinking!&lt;/P&gt;</description>
      <pubDate>Wed, 12 Sep 2018 04:12:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494711#M130424</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-09-12T04:12:41Z</dc:date>
    </item>
    <item>
      <title>Re: Getting new record with SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494814#M130470</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines missover;
input NAME :$5. BIRTHDATE :$8. SOURCE :$3. ADDRESS :$8.;
datalines;
ANGEL 09122018 OLD USA
ANGEL 09112018 NEW 
FELIX 07252017 OLD UK
FELIX 07252017 NEW
SARAH 09092018 OLD CANADA
;
run;


proc sql;
create table want as
select *,max(ADDRESS) as new_ADDRESS
from have
group by name
having SOURCE=min(SOURCE) ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Sep 2018 12:59:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/494814#M130470</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-09-12T12:59:55Z</dc:date>
    </item>
    <item>
      <title>Re: Getting new record with SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/495095#M130602</link>
      <description>&lt;P&gt;Thank you everyone here for presenting various solutions &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I like the Coalesce idea. I forgot about using it!&lt;/P&gt;</description>
      <pubDate>Thu, 13 Sep 2018 00:13:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-new-record-with-SQL/m-p/495095#M130602</guid>
      <dc:creator>angeliquec</dc:creator>
      <dc:date>2018-09-13T00:13:47Z</dc:date>
    </item>
  </channel>
</rss>

