<?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: Select rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776546#M246997</link>
    <description>&lt;P&gt;So this is the solution without using Hash, thank you for your advice&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Customer_id $ person_id $;
cards;
1 123
1 456
2 456
3 767
4 654 
5 999
5 888
6 231
6 763
6 124
7 000
7 111
8 000
9 555
;

data two one;
set have;
by customer_id;
if first.customer_id and last.customer_id
then output one;
else output two;
run;

/***Person_ID's from two that exists i one****/
proc sql;
create table with_one as
select distinct t1.customer_id, t1.person_id
from two t1, one t2
where t1.person_id = t2.person_id
order by customer_id;
quit;


proc sql;
create table as Final_List_From_one
select b.*
from with_one as a
inner join one as b
on a.person_id=b.person_id
;
quit;


proc sql;
create table as Final_List_From_two
select b.*
from with_one as a
inner join two as b
on a.customer_id=b.customer_id
;
quit;

Data wanted;
set  Final_List_From_one  
      Final_List_From_two;
Run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 26 Oct 2021 16:53:54 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2021-10-26T16:53:54Z</dc:date>
    <item>
      <title>Select rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776148#M246787</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;My raw data data set includes 2 columns: Customer_ID and person_ID.&lt;/P&gt;
&lt;P&gt;The information shows the owners&amp;nbsp; of each customer.&lt;/P&gt;
&lt;P&gt;For example: Customer 1 has 2 owners:123 and 456&lt;/P&gt;
&lt;P&gt;I want to select the rows from data set have by the following criteria:&lt;/P&gt;
&lt;P&gt;A customer that has 2 owners and one of the owners is owner (alone) of another customer.&lt;/P&gt;
&lt;P&gt;So in this example the following rows will be selected by this criteria:&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;1 123
1 456
2 456&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;7 000
7 111
8 000&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;My&amp;nbsp;question:&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;What&amp;nbsp;is&amp;nbsp;the&amp;nbsp;way&amp;nbsp;to&amp;nbsp;select&amp;nbsp;the&amp;nbsp;rows&amp;nbsp;by&amp;nbsp;the&amp;nbsp;criteria&amp;nbsp;that&amp;nbsp;I&amp;nbsp;have&amp;nbsp;mentioned&amp;nbsp;?&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input Customer_id person_id ;
cards;
1 123
1 456
2 456
3 767
4 654 
5 999
5 888
6 231
6 763
6 124
7 000
7 111
8 000
9 555
;
Run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Oct 2021 09:11:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776148#M246787</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-10-25T09:11:09Z</dc:date>
    </item>
    <item>
      <title>Re: Select rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776152#M246791</link>
      <description>&lt;P&gt;Customer_ID = 2 has only 1 owner?&lt;/P&gt;</description>
      <pubDate>Mon, 25 Oct 2021 09:27:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776152#M246791</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-10-25T09:27:17Z</dc:date>
    </item>
    <item>
      <title>Re: Select rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776154#M246793</link>
      <description>&lt;P&gt;Yes, customer_ID=2 has only 1 owner.&lt;/P&gt;
&lt;P&gt;I am looking for situation when a customer_ID has 2 owners and one of the owners belongs also to another customer_ID and he is alone there&lt;/P&gt;</description>
      <pubDate>Mon, 25 Oct 2021 09:40:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776154#M246793</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-10-25T09:40:17Z</dc:date>
    </item>
    <item>
      <title>Re: Select rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776155#M246794</link>
      <description>&lt;P&gt;So if there were 2 obs for person_id, we should not output in this case?&lt;/P&gt;</description>
      <pubDate>Mon, 25 Oct 2021 09:41:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776155#M246794</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-10-25T09:41:43Z</dc:date>
    </item>
    <item>
      <title>Re: Select rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776156#M246795</link>
      <description>The rows that need to be in output are&lt;BR /&gt;1 123&lt;BR /&gt;1 456&lt;BR /&gt;2 456&lt;BR /&gt;7 000&lt;BR /&gt;7 111&lt;BR /&gt;8 000&lt;BR /&gt;&lt;BR /&gt;because these rows meet the criteria:&lt;BR /&gt;There is a customer with 2 owners and one of these ID's belong to another customer and this customer has only 1 owner&lt;BR /&gt;</description>
      <pubDate>Mon, 25 Oct 2021 09:45:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776156#M246795</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-10-25T09:45:48Z</dc:date>
    </item>
    <item>
      <title>Re: Select rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776166#M246800</link>
      <description>&lt;P&gt;What if a customer has more than 2 owners?&lt;/P&gt;</description>
      <pubDate>Mon, 25 Oct 2021 10:47:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776166#M246800</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-10-25T10:47:53Z</dc:date>
    </item>
    <item>
      <title>Re: Select rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776248#M246829</link>
      <description>Then he shouldn't  be there .&lt;BR /&gt;I wrote the conditions:&lt;BR /&gt;1-one account with 2 owners&lt;BR /&gt;2- second account with 1 owner &lt;BR /&gt;3- The owner in second account is also an owner in first account &lt;BR /&gt;4- The owners don't  belong to any other account ( only first account and second account are in the ownerships).&lt;BR /&gt;This situation  is when a man+wife has a bank account  and the man also has a bank account  by himself...</description>
      <pubDate>Mon, 25 Oct 2021 17:27:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776248#M246829</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-10-25T17:27:32Z</dc:date>
    </item>
    <item>
      <title>Re: Select rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776409#M246926</link>
      <description>&lt;P&gt;This creates your want dataset:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Customer_id $ person_id $;
cards;
1 123
1 456
2 456
3 767
4 654 
5 999
5 888
6 231
6 763
6 124
7 000
7 111
8 000
9 555
;

data two one;
set have;
by customer_id;
if first.customer_id and last.customer_id
then output one;
else output two;
run;

proc sql;
create table with_one as
select distinct t1.customer_id, t1.person_id
from two t1, one t2
where t1.person_id = t2.person_id
order by customer_id;
quit;

data want;
set have;
if _n_ = 1
then do;
  declare hash two (dataset:"with_one");
  two.definekey("customer_id");
  two.definedone();
  declare hash one (dataset:"with_one");
  one.definekey("person_id");
  one.definedone();
end;
if two.check() = 0 or one.check() = 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Oct 2021 09:38:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776409#M246926</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-10-26T09:38:34Z</dc:date>
    </item>
    <item>
      <title>Re: Select rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776430#M246942</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;The&amp;nbsp; code you showed is perfect but when I applied it on the real data I get an error ,&lt;/P&gt;
&lt;PRE&gt;NOTE: Compression was disabled for data set WORK.WANT because compression overhead would increase the size of the data set.
ERROR: Undeclared key symbol customer_id  for hash object at line 34 column 3.
ERROR: DATA STEP Component Object failure.  Aborted during the EXECUTION phase.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: There were 1 observations read from the data set WORK.HAVE.
WARNING: The data set WORK.WANT may be incomplete.  When this step was stopped there were 0 observations and 2 variables.
WARNING: Data set WORK.WANT was not replaced because this step was stopped.
&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Oct 2021 11:59:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776430#M246942</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-10-26T11:59:56Z</dc:date>
    </item>
    <item>
      <title>Re: Select rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776435#M246944</link>
      <description>&lt;P&gt;My code works with the data as posted. I can only develop and test with data I have.&lt;/P&gt;
&lt;P&gt;Please post the complete log of all steps that I supplied.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Oct 2021 12:05:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776435#M246944</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-10-26T12:05:33Z</dc:date>
    </item>
    <item>
      <title>Re: Select rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776470#M246965</link>
      <description>&lt;P&gt;May you please show another solution without Hash methology?&lt;/P&gt;</description>
      <pubDate>Tue, 26 Oct 2021 13:15:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776470#M246965</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-10-26T13:15:33Z</dc:date>
    </item>
    <item>
      <title>Re: Select rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776475#M246969</link>
      <description>&lt;P&gt;Join the one and two datasets with the with_one dataset, based on the respective relevant keys, then concatenate the results. After 1200+ posts here, this will be a simple task you can do on your own.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Oct 2021 13:24:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776475#M246969</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-10-26T13:24:27Z</dc:date>
    </item>
    <item>
      <title>Re: Select rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776546#M246997</link>
      <description>&lt;P&gt;So this is the solution without using Hash, thank you for your advice&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Customer_id $ person_id $;
cards;
1 123
1 456
2 456
3 767
4 654 
5 999
5 888
6 231
6 763
6 124
7 000
7 111
8 000
9 555
;

data two one;
set have;
by customer_id;
if first.customer_id and last.customer_id
then output one;
else output two;
run;

/***Person_ID's from two that exists i one****/
proc sql;
create table with_one as
select distinct t1.customer_id, t1.person_id
from two t1, one t2
where t1.person_id = t2.person_id
order by customer_id;
quit;


proc sql;
create table as Final_List_From_one
select b.*
from with_one as a
inner join one as b
on a.person_id=b.person_id
;
quit;


proc sql;
create table as Final_List_From_two
select b.*
from with_one as a
inner join two as b
on a.customer_id=b.customer_id
;
quit;

Data wanted;
set  Final_List_From_one  
      Final_List_From_two;
Run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Oct 2021 16:53:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776546#M246997</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-10-26T16:53:54Z</dc:date>
    </item>
    <item>
      <title>Re: Select rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776550#M246998</link>
      <description>&lt;P&gt;I want yo ask also about your code please,&lt;/P&gt;
&lt;P&gt;In your code you used cartesian product .&lt;/P&gt;
&lt;PRE&gt;proc sql;
create table with_one as
select distinct t1.customer_id, t1.person_id
from two t1, one t2
where t1.person_id = t2.person_id
order by customer_id;
quit;&lt;/PRE&gt;
&lt;P&gt;Why didn't you use inner join? As I see both ways are giving same results , which is more efficient when have 10 million rows?&lt;/P&gt;
&lt;PRE&gt;proc sql;
create table with_one as
select  t1.customer_id, t1.person_id
from two t1
inner join  one t2
on t1.person_id = t2.person_id
order by customer_id;
quit;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Oct 2021 17:01:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776550#M246998</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-10-26T17:01:43Z</dc:date>
    </item>
    <item>
      <title>Re: Select rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776552#M246999</link>
      <description>&lt;P&gt;I'm used to the WHERE method from our DB/2 people. The optimizer there is most probably more powerful than that of PROC SQL &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;One day I will ask why they prefer it to inner joins.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Oct 2021 17:16:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776552#M246999</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-10-26T17:16:08Z</dc:date>
    </item>
    <item>
      <title>Re: Select rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776553#M247000</link>
      <description>Both are done via proc sql( inner join and Cartesian  product.&lt;BR /&gt;As I understand you say that Cartesian product is more efficient( using where )?</description>
      <pubDate>Tue, 26 Oct 2021 17:20:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776553#M247000</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-10-26T17:20:51Z</dc:date>
    </item>
    <item>
      <title>Re: Select rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776557#M247003</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Both are done via proc sql( inner join and Cartesian product.&lt;BR /&gt;As I understand you say that Cartesian product is more efficient( using where )?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;No. What is more efficient is best determined by testing (see Maxim 4).&lt;/P&gt;
&lt;P&gt;It is just that our database coders prefer to use the cartesian + WHERE, for whatever reason.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Oct 2021 17:49:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-rows/m-p/776557#M247003</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-10-26T17:49:11Z</dc:date>
    </item>
  </channel>
</rss>

