<?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: Retain both records if group is different otherwise keep the last in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Retain-both-records-if-group-is-different-otherwise-keep-the/m-p/702725#M215247</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/358432"&gt;@Stalk&lt;/a&gt;&amp;nbsp; I'm not sure of your expected output. Ho&lt;SPAN&gt;wever, rather than looking at your code, I'm taking a guess with your &lt;STRONG&gt;bold&lt;/STRONG&gt; lines and your description-&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;"I would like to output last. if the group value is same.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;If the group value is different I want to retain both records.(keep both Bold records below)"&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data to_clean;
infile cards dlm='|' truncover ;
input subDate :mmddyy10. unitName :$100. ADDR1 :$100. group $4.;
format subDate yymmdd10.;
cards;
11/21/2020|BAIRD HUDSON ENTERPRISES|106 E MAIN|TNF
10/30/2020|BAIRD HUDSON ENTERPRISES|106 E MAIN STREET|TNF
10/30/2020|BIG HORN ENTERPRISE|146 S. BENT STREET|SNF
10/30/2020|BIG HORN ENTERPRISE|641 WARREN STREET|SNF
11/5/2020|BROOKDALE |tt|ALF
10/29/2020|BROOKDALE|2401 COUGAR AVENUE|ALF
10/30/2020|ELMCROFT|1551 SUGARLAND DRIVE|ALF
11/2/2020|ELMCROFT|1551 SUGARLAND DRIVE DRIVE|SNF
11/21/2020|GREEN HOUSE LIVING|2311 SHIRLEY|SNF
10/29/2020|GREEN HOUSE LIVING|2311 SHIRLEY COVE|ALF
11/21/2020|MISSION AT THE VILLA|1445 UINTA|ALF
11/2/2020|MISSION AT THE VILLA|1445 UINTA DRIVE|ALF
;

data want;
 do _n_=1 by 1 until(last.unitname);
  set to_clean;
  by unitname group notsorted;
  if first.group then n=sum(n,1);
 end;
 if n=1 then output;
 else do _n_=1 to _n_;
  set to_clean;
  output;
 end;
 drop n;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 01 Dec 2020 03:54:53 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2020-12-01T03:54:53Z</dc:date>
    <item>
      <title>Retain both records if group is different otherwise keep the last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-both-records-if-group-is-different-otherwise-keep-the/m-p/702714#M215242</link>
      <description>&lt;P&gt;This is extension to my previous question.&lt;/P&gt;&lt;P&gt;I would like to output last. if the group value is same.&lt;/P&gt;&lt;P&gt;If the group value is different I want to retain both records.(keep both Bold records below)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data to_clean;&lt;BR /&gt;infile cards dlm='|' truncover ;&lt;BR /&gt;input subDate :mmddyy10. unitName :$100. ADDR1 :$100. group $4.;&lt;BR /&gt;format subDate yymmdd10.;&lt;BR /&gt;cards;&lt;BR /&gt;11/21/2020|BAIRD HUDSON ENTERPRISES|106 E MAIN|TNF&lt;BR /&gt;10/30/2020|BAIRD HUDSON ENTERPRISES|106 E MAIN STREET|TNF&lt;BR /&gt;10/30/2020|BIG HORN ENTERPRISE|146 S. BENT STREET|SNF&lt;BR /&gt;10/30/2020|BIG HORN ENTERPRISE|641 WARREN STREET|SNF&lt;BR /&gt;11/5/2020|BROOKDALE |tt|ALF&lt;BR /&gt;10/29/2020|BROOKDALE|2401 COUGAR AVENUE|ALF&lt;BR /&gt;&lt;STRONG&gt;10/30/2020|ELMCROFT|1551 SUGARLAND DRIVE|ALF&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;11/2/2020|ELMCROFT|1551 SUGARLAND DRIVE DRIVE|SNF&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;11/21/2020|GREEN HOUSE LIVING|2311 SHIRLEY|SNF&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;10/29/2020|GREEN HOUSE LIVING|2311 SHIRLEY COVE|ALF&lt;/STRONG&gt;&lt;BR /&gt;11/21/2020|MISSION AT THE VILLA|1445 UINTA|ALF&lt;BR /&gt;11/2/2020|MISSION AT THE VILLA|1445 UINTA DRIVE|ALF&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;proc sort data=to_clean; by unitName subDate; run;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table to_clean_new as&lt;BR /&gt;select * from to_clean&lt;BR /&gt;order by unitName, subDate, group;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data to_clean_2;&lt;BR /&gt;set to_clean_new;&lt;BR /&gt;by unitName;&lt;BR /&gt;length goodAddr $100;&lt;BR /&gt;retain goodAddr;&lt;BR /&gt;if first.unitName then goodAddr = addr1;&lt;BR /&gt;else if complev(trim(goodAddr), trim(addr1), "IL:") &amp;gt; 2 then goodAddr = addr1;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc sort data=to_clean_2; by unitName goodAddr subDate group; run;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;set to_clean_2;&lt;BR /&gt;by unitName goodAddr;&lt;BR /&gt;if last.goodAddr;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc print noobs data=want; run;&lt;/P&gt;&lt;P&gt;****&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Tue, 01 Dec 2020 03:11:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-both-records-if-group-is-different-otherwise-keep-the/m-p/702714#M215242</guid>
      <dc:creator>Stalk</dc:creator>
      <dc:date>2020-12-01T03:11:32Z</dc:date>
    </item>
    <item>
      <title>Re: Retain both records if group is different otherwise keep the last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-both-records-if-group-is-different-otherwise-keep-the/m-p/702725#M215247</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/358432"&gt;@Stalk&lt;/a&gt;&amp;nbsp; I'm not sure of your expected output. Ho&lt;SPAN&gt;wever, rather than looking at your code, I'm taking a guess with your &lt;STRONG&gt;bold&lt;/STRONG&gt; lines and your description-&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;"I would like to output last. if the group value is same.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;If the group value is different I want to retain both records.(keep both Bold records below)"&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data to_clean;
infile cards dlm='|' truncover ;
input subDate :mmddyy10. unitName :$100. ADDR1 :$100. group $4.;
format subDate yymmdd10.;
cards;
11/21/2020|BAIRD HUDSON ENTERPRISES|106 E MAIN|TNF
10/30/2020|BAIRD HUDSON ENTERPRISES|106 E MAIN STREET|TNF
10/30/2020|BIG HORN ENTERPRISE|146 S. BENT STREET|SNF
10/30/2020|BIG HORN ENTERPRISE|641 WARREN STREET|SNF
11/5/2020|BROOKDALE |tt|ALF
10/29/2020|BROOKDALE|2401 COUGAR AVENUE|ALF
10/30/2020|ELMCROFT|1551 SUGARLAND DRIVE|ALF
11/2/2020|ELMCROFT|1551 SUGARLAND DRIVE DRIVE|SNF
11/21/2020|GREEN HOUSE LIVING|2311 SHIRLEY|SNF
10/29/2020|GREEN HOUSE LIVING|2311 SHIRLEY COVE|ALF
11/21/2020|MISSION AT THE VILLA|1445 UINTA|ALF
11/2/2020|MISSION AT THE VILLA|1445 UINTA DRIVE|ALF
;

data want;
 do _n_=1 by 1 until(last.unitname);
  set to_clean;
  by unitname group notsorted;
  if first.group then n=sum(n,1);
 end;
 if n=1 then output;
 else do _n_=1 to _n_;
  set to_clean;
  output;
 end;
 drop n;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Dec 2020 03:54:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-both-records-if-group-is-different-otherwise-keep-the/m-p/702725#M215247</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-12-01T03:54:53Z</dc:date>
    </item>
    <item>
      <title>Re: Retain both records if group is different otherwise keep the last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-both-records-if-group-is-different-otherwise-keep-the/m-p/702822#M215279</link>
      <description>Records with different group are not captured in the output dataset. From the data above I would like to get 1,3,4,5, 7,8,9,10 observations( eliminate 2 , 6 and 11).</description>
      <pubDate>Tue, 01 Dec 2020 15:35:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-both-records-if-group-is-different-otherwise-keep-the/m-p/702822#M215279</guid>
      <dc:creator>Stalk</dc:creator>
      <dc:date>2020-12-01T15:35:17Z</dc:date>
    </item>
    <item>
      <title>Re: Retain both records if group is different otherwise keep the last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-both-records-if-group-is-different-otherwise-keep-the/m-p/702842#M215291</link>
      <description>&lt;P&gt;I've restated your objective to "keep the last observation (highest subdate) for each unitname/group combination).&amp;nbsp; If that's correct, then this application of proc summary and a subsequent use of a hash object will do, which prunes the dataset while preserving the original order of the data (in case that's important).&amp;nbsp; It requires only 2 passes through the data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data to_clean;
infile cards dlm='|' truncover ;
input subDate :mmddyy10. unitName :$100. ADDR1 :$100. group $4.;
format subDate yymmdd10.;
cards;
11/21/2020|BAIRD HUDSON ENTERPRISES|106 E MAIN|TNF
10/30/2020|BAIRD HUDSON ENTERPRISES|106 E MAIN STREET|TNF
10/30/2020|BIG HORN ENTERPRISE|146 S. BENT STREET|SNF
10/30/2020|BIG HORN ENTERPRISE|641 WARREN STREET|SNF
11/5/2020|BROOKDALE |tt|ALF
10/29/2020|BROOKDALE|2401 COUGAR AVENUE|ALF
10/30/2020|ELMCROFT|1551 SUGARLAND DRIVE|ALF
11/2/2020|ELMCROFT|1551 SUGARLAND DRIVE DRIVE|SNF
11/21/2020|GREEN HOUSE LIVING|2311 SHIRLEY|SNF
10/29/2020|GREEN HOUSE LIVING|2311 SHIRLEY COVE|ALF
11/21/2020|MISSION AT THE VILLA|1445 UINTA|ALF
11/2/2020|MISSION AT THE VILLA|1445 UINTA DRIVE|ALF
;

proc summary data=to_clean nway;
  class unitname group;
  var subdate;
  output out=need (drop=_:) max=subdate;
run;

data want;
  set to_clean;
  if _n_=1 then do;
    declare hash h (dataset:'need');
	  h.definekey(all:'Y');
	  h.definedone();
  end;
  if h.check()=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The proc summary (because of the NWAY option) outputs a dataset with one observation for each unitname/group combination.&amp;nbsp; It records the maximum value of the analysis variable subdate for each combo, thereby providing the desired unitname/group/subdate values.&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Stick that dataset in the hash object h, and check each incoming obs from to_clean against h, and keep only those whose key values (unitname,group,subdate) are found in h (i.e. "if h.check()=0").&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Dec 2020 17:29:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-both-records-if-group-is-different-otherwise-keep-the/m-p/702842#M215291</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-12-01T17:29:40Z</dc:date>
    </item>
    <item>
      <title>Re: Retain both records if group is different otherwise keep the last</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Retain-both-records-if-group-is-different-otherwise-keep-the/m-p/703131#M215419</link>
      <description>Thank you mkeintz for suggesting Hash object. I really don't understand how hash object works so avoid using that. But I got my program working for the desired results with simple fixxes to sort..&lt;BR /&gt;&lt;BR /&gt;proc sort data=to_clean_2; by unitName goodAddr group descending subDate ; run;&lt;BR /&gt;&lt;BR /&gt;data new;&lt;BR /&gt;set to_clean_2;&lt;BR /&gt;by unitname goodaddr group;&lt;BR /&gt;if first.group then output;&lt;BR /&gt;run;</description>
      <pubDate>Wed, 02 Dec 2020 16:00:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Retain-both-records-if-group-is-different-otherwise-keep-the/m-p/703131#M215419</guid>
      <dc:creator>Stalk</dc:creator>
      <dc:date>2020-12-02T16:00:43Z</dc:date>
    </item>
  </channel>
</rss>

