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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 1571 views
  • 2 likes
  • 3 in conversation