BookmarkSubscribeRSS Feed
echoli
Obsidian | Level 7

Hi All,

 

What should I do if I want to extract all the observations have same value but may in different two rows in two columns. For example:

Col_1      Col_2

1              1

2              2

3              3

4              5

5              6

6              7

7              9

8              10

The red color records are observations I want to extract. I am using SAS 9.4. 

 

Any idea?

 

Thanks,

C

 

2 REPLIES 2
Astounding
PROC Star

No promises (in fact, not even tested), but this might work:

 

proc sql;

create table want as select col_1, col_2 from have

where col_1 in (select distinct col_2 from have)

or col_2 in (select distinct col_1 from have);

quit;

Ksharp
Super User
data have;
input Col_1      Col_2;
cards;
1              1
2              2
3              3
4              5
5              6
6              7
7              9
8              10
;
run;

data want;
 if _n_=1 then do;
  if 0 then set have;
  declare hash h1(dataset:'have');
  h1.definekey(key:'col_1');
  h1.definedone();
  
  declare hash h2(dataset:'have');
  h2.definekey(key:'col_2');
  h2.definedone();
 end;
set have;
if h1.check(key:col_2)=0 or h2.check(key:col_1)=0;
run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 1827 views
  • 2 likes
  • 3 in conversation