<?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: keep a rows which belongs to a group of data if one of them satisfy the requirement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/keep-a-rows-which-belongs-to-a-group-of-data-if-one-of-them/m-p/524812#M142733</link>
    <description>&lt;P&gt;Hi France&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As I understand you, you want all records for a given company, if the company is associated with GB.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With respect for the suggestions already given, I would recommend a simple two-step approace: First find the relevant companies, and then get all records for these companies.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following code wil give you that. If you want to include UK as well (if no GB record exists), just replace the where statements with the one in comments.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards dsd truncover;
input company_name $ country $;
cards;
Tesco,GB
Tesco,US
Tesco, 
apple,US
apple, 
apple,UK
sony,Japan 
tesco,UK
;

proc sql;
	create table temp as
		select distinct company_name
		from have
		where country = 'GB';
	/*	where country = 'GB' or country = 'UK';*/

	create table want as
		select *
		from have
		where company_name in (select company_name from temp);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 05 Jan 2019 14:07:12 GMT</pubDate>
    <dc:creator>ErikLund_Jensen</dc:creator>
    <dc:date>2019-01-05T14:07:12Z</dc:date>
    <item>
      <title>keep a rows which belongs to a group of data if one of them satisfy the requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-a-rows-which-belongs-to-a-group-of-data-if-one-of-them/m-p/524623#M142677</link>
      <description>&lt;P&gt;Dear all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;how can I keep a group of data if one of them satisfy the requirement?&lt;/P&gt;&lt;P&gt;for example&amp;nbsp;&lt;/P&gt;&lt;P&gt;table 1&lt;/P&gt;&lt;P&gt;company_name, country&lt;/P&gt;&lt;P&gt;apple,US&lt;/P&gt;&lt;P&gt;apple,&amp;nbsp;&lt;/P&gt;&lt;P&gt;apple,UK&lt;/P&gt;&lt;P&gt;tesco,UK&lt;/P&gt;&lt;P&gt;sony,Japan&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to keep all rows&amp;nbsp;relevant with company&amp;nbsp;'apple' and 'Tesco' if a country recorded with them is UK&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;table 2&lt;/P&gt;&lt;P&gt;company_name, country&lt;/P&gt;&lt;P&gt;apple,US&lt;/P&gt;&lt;P&gt;apple,&amp;nbsp;&lt;/P&gt;&lt;P&gt;apple,UK&lt;/P&gt;&lt;P&gt;tesco,UK&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;could you please give me some suggestion about this?&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Jan 2019 17:15:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-a-rows-which-belongs-to-a-group-of-data-if-one-of-them/m-p/524623#M142677</guid>
      <dc:creator>France</dc:creator>
      <dc:date>2019-01-04T17:15:47Z</dc:date>
    </item>
    <item>
      <title>Re: keep a rows which belongs to a group of data if one of them satisfy the requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-a-rows-which-belongs-to-a-group-of-data-if-one-of-them/m-p/524626#M142678</link>
      <description>&lt;P&gt;Assuming I understand your requirement--&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
infile cards dsd truncover;
input company_name $ country $;
cards;
apple,US
apple, 
apple,UK
tesco,UK
sony,Japan 
;
proc sql;
create table want(drop=t) as
select *,max(country='UK' and company_name in ('apple','tesco')) as t
from have
having t and company_name in ('apple','tesco');
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 Jan 2019 17:27:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-a-rows-which-belongs-to-a-group-of-data-if-one-of-them/m-p/524626#M142678</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-01-04T17:27:56Z</dc:date>
    </item>
    <item>
      <title>Re: keep a rows which belongs to a group of data if one of them satisfy the requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-a-rows-which-belongs-to-a-group-of-data-if-one-of-them/m-p/524647#M142685</link>
      <description>&lt;P&gt;Dear novinosrin,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your advice.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, my sample covers thousands of companies.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;besides, I would like to explain my requirements.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I expect to include all companies which are relevant with 'GB' country. and there are two types of revelations.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the first is that the 'company_name' is directly related to&amp;nbsp;'GB' country, such as&amp;nbsp;&lt;/P&gt;&lt;P&gt;company_name, country&lt;/P&gt;&lt;P&gt;Tesco, GB&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to include this row in the new dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the second is, the 'company_name' is related with&amp;nbsp;at least two country records(i.e., other country&amp;nbsp;code or blank), and one of them is 'GB' country, for example,&lt;/P&gt;&lt;P&gt;company_name, country&lt;/P&gt;&lt;P&gt;Tesco,GB&lt;/P&gt;&lt;P&gt;Tesco,US&lt;/P&gt;&lt;P&gt;Tesco,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to include all these three rows in the new dataset because the company names in the second and third row are as same as the company name in the first row which recorded with 'GB' country.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you please give me suggestions based on this requirement?&lt;/P&gt;&lt;P&gt;many thanks in advance.&lt;/P&gt;</description>
      <pubDate>Fri, 04 Jan 2019 18:45:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-a-rows-which-belongs-to-a-group-of-data-if-one-of-them/m-p/524647#M142685</guid>
      <dc:creator>France</dc:creator>
      <dc:date>2019-01-04T18:45:53Z</dc:date>
    </item>
    <item>
      <title>Re: keep a rows which belongs to a group of data if one of them satisfy the requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-a-rows-which-belongs-to-a-group-of-data-if-one-of-them/m-p/524654#M142689</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/194466"&gt;@France&lt;/a&gt;&amp;nbsp; Ok, try this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards dsd truncover;
input company_name $ country $;
cards;
apple,US
apple, 
apple,UK
tesco,UK
sony,Japan 
;

proc sql;
create table want(drop=t) as
select *,count(distinct country)&amp;gt;1 and sum(country='UK')&amp;gt;=1 as t
from have
group by company_name
having t or country='UK';
quit;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Logic:&lt;/P&gt;
&lt;P&gt;1. check whether each company whether is present n the UK and is present in any other country&lt;/P&gt;
&lt;P&gt;2. If 1 is true,output that&lt;/P&gt;
&lt;P&gt;3. If the company is only present in UK and not in any other country, output that too&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the above is correct, it's still piece of cake&lt;/P&gt;</description>
      <pubDate>Fri, 04 Jan 2019 19:00:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-a-rows-which-belongs-to-a-group-of-data-if-one-of-them/m-p/524654#M142689</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-01-04T19:00:15Z</dc:date>
    </item>
    <item>
      <title>Re: keep a rows which belongs to a group of data if one of them satisfy the requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-a-rows-which-belongs-to-a-group-of-data-if-one-of-them/m-p/524663#M142694</link>
      <description>&lt;P&gt;Or Datastep&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
infile cards dsd truncover;
input company_name $ country $;
cards;
apple,US
apple, 
apple,UK
tesco,UK
sony,Japan 
;
proc sort data=have;
by company_name;
run;

data want;
do until(last.company_name);
set have;
by company_name ;
if country='UK' then f=1;
end;
do until(last.company_name);
set have;
by company_name ;
if  f then output;
end;
drop f;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 Jan 2019 19:21:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-a-rows-which-belongs-to-a-group-of-data-if-one-of-them/m-p/524663#M142694</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-01-04T19:21:17Z</dc:date>
    </item>
    <item>
      <title>Re: keep a rows which belongs to a group of data if one of them satisfy the requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-a-rows-which-belongs-to-a-group-of-data-if-one-of-them/m-p/524671#M142699</link>
      <description>&lt;P&gt;Even simpler&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards dsd truncover;
input company_name $ country $;
cards;
apple,US
apple, 
apple,UK
tesco,UK
sony,Japan 
;
proc sort data=have;
by company_name;
run;

data want;
merge have(in=a where=(country='UK')) have(in=b);
by company_name;
if a and b ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or a join&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 a.*
from have a inner join  have(where=(country='UK')) b
on a.company_name=b.company_name;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 Jan 2019 19:32:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-a-rows-which-belongs-to-a-group-of-data-if-one-of-them/m-p/524671#M142699</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-01-04T19:32:33Z</dc:date>
    </item>
    <item>
      <title>Re: keep a rows which belongs to a group of data if one of them satisfy the requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-a-rows-which-belongs-to-a-group-of-data-if-one-of-them/m-p/524778#M142724</link>
      <description>&lt;P&gt;If your data are already sorted by company_name (but the UK entry appears randomly within the duplicate company_name group, then (note I've sorted the sample data):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards dsd truncover;
input company_name $ country $;
cards;
apple,US
apple, 
apple,UK
sony,Japan 
tesco,UK
;

data want;
  merge have (where=(country='UK') in=ingb) have;
  by company_name;
  if ingb;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The MERGE does many-to-one matches.&amp;nbsp; So the &lt;EM&gt;&lt;STRONG&gt;where=(country='UK')&lt;/STRONG&gt;&lt;/EM&gt; single entry will match all entry for the same company_name.&amp;nbsp; And if there are multiple UK entries (say 2) for a company, then the last 2nd such entry will be matched against the 2nd and all subsequent entries for that company.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also the merge statement will&amp;nbsp;overwrite value in the left entry with values from the right entry.&amp;nbsp; So the UK values will be overwritten by the values coming from the right-hand have records.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now if the data are not sorted by company_name, and sorting is expensive, then (date here is unsorted):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards dsd truncover;
input company_name $ country $;
cards;
apple,US
apple, 
tesco,UK
sony,Japan 
apple,UK
;

data want;
  set have;
  if _n_=1 then do;
    declare hash h (dataset:"have (where=(country='UK'))");
	  h.definekey('company_name');
	  h.definedone();
  end;
  if h.check()=0;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This program builds a hash object H, keyed on company_name (meaning you can do a table lookup by company_name).&amp;nbsp; But the only company_name values in H are those with country='UK'.&amp;nbsp;&amp;nbsp; Note, the H object is retained across observations, so it is instantiated only once, namely when the first iteration of the data step is executed - at which time the entire dataset is processed once for building the hash object.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The h.check() method returns a zero only when successful, i.e. only when the incoming company_name is found in h.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note this program effectively reads the data set HAVE twice - once to build H, and once via the normal data step processing sequence.&amp;nbsp; Even so, it's cheaper than sorting then data, followed by another data step - and possibly by another sort to reproduce original order, if needed.&lt;/P&gt;</description>
      <pubDate>Sat, 05 Jan 2019 01:31:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-a-rows-which-belongs-to-a-group-of-data-if-one-of-them/m-p/524778#M142724</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-01-05T01:31:28Z</dc:date>
    </item>
    <item>
      <title>Re: keep a rows which belongs to a group of data if one of them satisfy the requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-a-rows-which-belongs-to-a-group-of-data-if-one-of-them/m-p/524792#M142729</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards dsd truncover;
input company_name $ country $;
cards;
apple,US
apple, 
apple,UK
sony,Japan 
tesco,UK
;

proc sql;
select *
 from have
  group by company_name
   having sum(country='UK') ne 0;
  quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 05 Jan 2019 10:26:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-a-rows-which-belongs-to-a-group-of-data-if-one-of-them/m-p/524792#M142729</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-01-05T10:26:53Z</dc:date>
    </item>
    <item>
      <title>Re: keep a rows which belongs to a group of data if one of them satisfy the requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-a-rows-which-belongs-to-a-group-of-data-if-one-of-them/m-p/524802#M142731</link>
      <description>&lt;P&gt;Would definitely go with a hash solution&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  if _n_=1 then do;
     declare hash h (dataset:"have(where=(country='UK'))");
	  h.definekey('company_name');
	  h.definedone();
  end;

  set have;
  if h.check()=0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 05 Jan 2019 12:23:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-a-rows-which-belongs-to-a-group-of-data-if-one-of-them/m-p/524802#M142731</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-01-05T12:23:26Z</dc:date>
    </item>
    <item>
      <title>Re: keep a rows which belongs to a group of data if one of them satisfy the requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keep-a-rows-which-belongs-to-a-group-of-data-if-one-of-them/m-p/524812#M142733</link>
      <description>&lt;P&gt;Hi France&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As I understand you, you want all records for a given company, if the company is associated with GB.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With respect for the suggestions already given, I would recommend a simple two-step approace: First find the relevant companies, and then get all records for these companies.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following code wil give you that. If you want to include UK as well (if no GB record exists), just replace the where statements with the one in comments.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards dsd truncover;
input company_name $ country $;
cards;
Tesco,GB
Tesco,US
Tesco, 
apple,US
apple, 
apple,UK
sony,Japan 
tesco,UK
;

proc sql;
	create table temp as
		select distinct company_name
		from have
		where country = 'GB';
	/*	where country = 'GB' or country = 'UK';*/

	create table want as
		select *
		from have
		where company_name in (select company_name from temp);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 05 Jan 2019 14:07:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keep-a-rows-which-belongs-to-a-group-of-data-if-one-of-them/m-p/524812#M142733</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-01-05T14:07:12Z</dc:date>
    </item>
  </channel>
</rss>

