<?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 select the more comprehensive row by a key column? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-more-comprehensive-row-by-a-key-column/m-p/592032#M169706</link>
    <description>&lt;P&gt;Using a hash:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set have end=done;
if _n_ = 1 then do;
	declare hash h();
	rc = h.definekey("fruit");
	rc = h.definedata("fruit", "attrib");
	rc = h.definedone();
	end;
if h.check() ne 0 then h.add();
else if not missing(attrib) then h.replace();
if done then h.output(dataset : "want");
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It will keep the last attribute encountered for a given fruit.&lt;/P&gt;</description>
    <pubDate>Thu, 26 Sep 2019 21:08:54 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2019-09-26T21:08:54Z</dc:date>
    <item>
      <title>How to select the more comprehensive row by a key column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-more-comprehensive-row-by-a-key-column/m-p/591964#M169679</link>
      <description>&lt;P&gt;Hello experts,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Suppose I have a table like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input fruit$ attrib$;
datalines;
apple
apple sweet
lemon sour
banana
banana
orange citrus
orange citrus
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So in this dataset, a fruit can sometimes have attribute that is complete (like orange), empty (like banana), or somewhere in between (like apple).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The challenge here is that I want to condense the rows into 4 rows, such that if the attribute for fruit X is provided in any row, I will keep that attribute for fruit X. If no attribute is provided for fruit X, I will leave it as is. So the resulting dataset will look like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;apple sweet&lt;/P&gt;&lt;P&gt;lemon sour&lt;/P&gt;&lt;P&gt;banana&lt;/P&gt;&lt;P&gt;orange citrus&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas will be appreciated.&lt;/P&gt;&lt;P&gt;Thank you,&lt;/P&gt;</description>
      <pubDate>Thu, 26 Sep 2019 18:51:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-more-comprehensive-row-by-a-key-column/m-p/591964#M169679</guid>
      <dc:creator>aaronh</dc:creator>
      <dc:date>2019-09-26T18:51:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the more comprehensive row by a key column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-more-comprehensive-row-by-a-key-column/m-p/591967#M169680</link>
      <description>do a distinct by fruit and another distinct by fruit and attrib.  Join the 2 together.</description>
      <pubDate>Thu, 26 Sep 2019 19:00:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-more-comprehensive-row-by-a-key-column/m-p/591967#M169680</guid>
      <dc:creator>tomrvincent</dc:creator>
      <dc:date>2019-09-26T19:00:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the more comprehensive row by a key column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-more-comprehensive-row-by-a-key-column/m-p/591968#M169681</link>
      <description>&lt;P&gt;Use Sort.&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 fruit descending attrib;
run;

proc sort data=have out=want nodupkey;
by fruit;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you have fruits with different attributes you may run into some issues.&amp;nbsp;&lt;/P&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/274317"&gt;@aaronh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello experts,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Suppose I have a table like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input fruit$ attrib$;
datalines;
apple
apple sweet
lemon sour
banana
banana
orange citrus
orange citrus
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So in this dataset, a fruit can sometimes have attribute that is complete (like orange), empty (like banana), or somewhere in between (like apple).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The challenge here is that I want to condense the rows into 4 rows, such that if the attribute for fruit X is provided in any row, I will keep that attribute for fruit X. If no attribute is provided for fruit X, I will leave it as is. So the resulting dataset will look like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;apple sweet&lt;/P&gt;
&lt;P&gt;lemon sour&lt;/P&gt;
&lt;P&gt;banana&lt;/P&gt;
&lt;P&gt;orange citrus&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any ideas will be appreciated.&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Sep 2019 19:04:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-more-comprehensive-row-by-a-key-column/m-p/591968#M169681</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-09-26T19:04:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the more comprehensive row by a key column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-more-comprehensive-row-by-a-key-column/m-p/591969#M169682</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;


data have;
input fruit$ attrib$;
datalines;
apple .
apple sweet
lemon sour
banana .
banana .
orange citrus
orange citrus
;

proc sql;
create table want as
select distinct a.*
from have a left join have b
on find(cats(a.fruit,a.attrib), strip(b.fruit))&amp;gt;0
group by a.fruit
having max(length(cats(a.fruit,a.attrib))) =length(cats(a.fruit,a.attrib));
quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 Sep 2019 19:06:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-more-comprehensive-row-by-a-key-column/m-p/591969#M169682</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-26T19:06:17Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the more comprehensive row by a key column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-more-comprehensive-row-by-a-key-column/m-p/591971#M169683</link>
      <description>Thanks Reeza! I was thinking of maybe using a sort first and then using a by group in a data step to choose the first/last in the by group. Which is somewhat similar I guess to what you have</description>
      <pubDate>Thu, 26 Sep 2019 19:12:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-more-comprehensive-row-by-a-key-column/m-p/591971#M169683</guid>
      <dc:creator>aaronh</dc:creator>
      <dc:date>2019-09-26T19:12:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the more comprehensive row by a key column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-more-comprehensive-row-by-a-key-column/m-p/592032#M169706</link>
      <description>&lt;P&gt;Using a hash:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set have end=done;
if _n_ = 1 then do;
	declare hash h();
	rc = h.definekey("fruit");
	rc = h.definedata("fruit", "attrib");
	rc = h.definedone();
	end;
if h.check() ne 0 then h.add();
else if not missing(attrib) then h.replace();
if done then h.output(dataset : "want");
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It will keep the last attribute encountered for a given fruit.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Sep 2019 21:08:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-more-comprehensive-row-by-a-key-column/m-p/592032#M169706</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-09-26T21:08:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the more comprehensive row by a key column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-more-comprehensive-row-by-a-key-column/m-p/592075#M169724</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/274317"&gt;@aaronh&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;Hm. If you real data are patterned the same way you've presented in your data sample, i.e. &lt;EM&gt;grouped&lt;/EM&gt; by [&lt;EM&gt;fruit,attrib&lt;/EM&gt;] with the missing values for &lt;EM&gt;attrib&lt;/EM&gt; coming first, the only thing you need to do is:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                
  input fruit $ attrib $ ; 
  cards ;                  
apple  .                   
apple  sweet               
lemon  sour                
banana .                   
banana .                   
orange citrus              
orange citrus              
;                          
run ;                      
                           
data want ;                
  set have ;               
  by fruit notsorted ;     
  if last.fruit ;          
run ;                      
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the real input data aren't grouped or sorted and you don't want to sort, just use what&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;or&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;have offered.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Sep 2019 04:05:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-more-comprehensive-row-by-a-key-column/m-p/592075#M169724</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-09-27T04:05:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the more comprehensive row by a key column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-more-comprehensive-row-by-a-key-column/m-p/592150#M169748</link>
      <description>&lt;P&gt;Thank you all very much for your insightful solutions!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;Yes, my real data is actually a bit more complex because I am trying to select the more comprehensive set of addresses (street address 1 and street address 2) for a by-group that consists of a few variables. It seems like using by-group in a data step would be the easiest way to comprehend or trouble-shoot.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;: thank you both for the solutions! At first glance, it seems like a simple logic to a human mind, but to implement it in SAS, it can be much more complicated.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Once again, I really appreciate the inputs from you all!&lt;/P&gt;</description>
      <pubDate>Fri, 27 Sep 2019 13:07:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-more-comprehensive-row-by-a-key-column/m-p/592150#M169748</guid>
      <dc:creator>aaronh</dc:creator>
      <dc:date>2019-09-27T13:07:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the more comprehensive row by a key column?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-more-comprehensive-row-by-a-key-column/m-p/592288#M169806</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
	CREATE TABLE WORK.foo AS 
		SELECT f.fruit, 
			a.attrib
		FROM (SELECT DISTINCT fruit FROM USER.HAVE) f
			FULL JOIN (SELECT DISTINCT fruit, attrib FROM USER.HAVE WHERE attrib NOT IS MISSING) a 
				ON f.fruit = a.fruit;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 27 Sep 2019 19:38:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-more-comprehensive-row-by-a-key-column/m-p/592288#M169806</guid>
      <dc:creator>tomrvincent</dc:creator>
      <dc:date>2019-09-27T19:38:18Z</dc:date>
    </item>
  </channel>
</rss>

