BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Rohit_1990
Calcite | Level 5

Hi All,

I am trying to create a dataset from a file such that if party has already in cluster it can not be a part of any other cluster.

Party1 party2

1 2

1 7

2 1

2 8

3 5

3 8

7 3

8 7

 

So basically we will start by smallest party1 and all corresponding party2 for that party1 are part of cluster.

But for party1= 1 , 2 and 7 are already part of cluster hence all cases with party1  syartist with 2and 7 will be eliminated and smallest next party 1will be considered for cluster .so next cluster will be 3 and so on

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

If I understood your question.

 

data have;
input Party1 party2;
cards;
1 2
1 7
2 1
2 8
3 5
3 8
7 3
8 7
;
run;

data want;
 if _n_=1 then do;
  if 0 then set have;
  declare hash h();
  h.definekey('party2');
  h.definedone();
 end;
set have;
if h.check(key:party1)=0 then delete;
h.replace();
run;

proc print;run;

View solution in original post

6 REPLIES 6
mkeintz
PROC Star

You've shown us the input data, and made a description of the requirements.

 

Help us help you.  Show how you want the result data set or table to appear.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
PGStats
Opal | Level 21

Does your example data implies that 1, 2, 3, 5, 7, 8 are all part of the same cluster?

PG
Rohit_1990
Calcite | Level 5
Output data set would be
Party1 party 2
1 2
1 7
3 5
3 8
Ksharp
Super User

If I understood your question.

 

data have;
input Party1 party2;
cards;
1 2
1 7
2 1
2 8
3 5
3 8
7 3
8 7
;
run;

data want;
 if _n_=1 then do;
  if 0 then set have;
  declare hash h();
  h.definekey('party2');
  h.definedone();
 end;
set have;
if h.check(key:party1)=0 then delete;
h.replace();
run;

proc print;run;
Rohit_1990
Calcite | Level 5

Hi Ksharp ,

 

your response is perfect as per my concern but I missed to highlight one scenario in my query.

 

 

data have;

input Party1 party2;

cards;

1 2

1 7

2 1

2 8

2 9

3 5

3 8

7 3

8 7

9 1

;

run;

 

I have included one more row in cluster for prty1=2 i.e. 2 9

Actually I want to excude all rows starting with prty2 values for my valid cluster.

since cluster starting with part1 =1 is valid cluster hence
all rows satrting with 2 and 7 will be discarded.

But since cluster starting with 2 was never considered to be a valid cluster
so prty2 values for this cluster rows will not be considered for row elimnation

so my final op will be something like

Party1 party2;


1 2


1 7


3 5


3 8


9 1

 

 

Thanks a ton in advance!!!!!!!!!!!!!!!!!!!!!!!

 

 

 

 

Ksharp
Super User

OK. How about this one ?

 

data have;
input Party1 party2;
cards;
1 2
1 7
2 1
2 8
2 9
3 5
3 8
7 3
8 7
9 1
;
run;

 
data want;
 if _n_=1 then do;
  if 0 then set have;
  declare hash h();
  h.definekey('party2');
  h.definedone();
 end;
set have;
if h.check(key:party1)=0 then delete;
 else h.replace();
run;

proc print;run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 698 views
  • 0 likes
  • 4 in conversation