Help using Base SAS procedures

How to Merge by Minimum Number of Observations in By Group?

Accepted Solution Solved
Reply
Contributor
Posts: 23
Accepted Solution

How to Merge by Minimum Number of Observations in By Group?

[ Edited ]

If I'm mot mistake, merge will, by default, make the total number of observations in the final data set set as the sum of the maximum number of observations in a BY-group from either data set.

 

How do I make the total number of observations in the final data set as the sum of the MINIMUM number of observations in the BY group from either data set?

 

I have this:

ID region

1  A

1 A

2  A

2 A

3  B

3 B

4  C

 

and

ID  size

1  5

1  6

1  7

2  3

2  4

3  4

3  5

4  5

 

& I'd like:

ID region size

1 A 5

1 A 6

2 A 3

2 A 4

3 B 4

3 B 5

4 C 5

 

Thank you in advance. 


Accepted Solutions
Solution
‎03-16-2016 05:42 PM
Super User
Posts: 5,092

Re: How to Merge by Minimum Number of Observations in By Group?

data minimum;

in1=0;

in2=0;

merge a (in=in1) b (in=in2);

by id;

if in1 and in2;

run;

View solution in original post


All Replies
Super User
Posts: 5,260

Re: How to Merge by Minimum Number of Observations in By Group?

How do you know that the first row in the "size" data set is the right one?

If you know, just use

if first.ID;

Assumng that there are no duplicates of ID in the "region" data set.

Data never sleeps
Contributor
Posts: 23

Re: How to Merge by Minimum Number of Observations in By Group?

Thank you.  I should elaborate.  My first data set is actually similar to this:

 

1 A

1 A

2 A

2 A

3 B

3 B

4 C

4 C

 

And I'd like to match by the number of observations in this dataset. However, if I use first.id, then it only matches by the very first observation.

Super User
Posts: 5,260

Re: How to Merge by Minimum Number of Observations in By Group?

Perhaps you should share some real data, because this logic makes little sense.

Merging data without proper keys (unique by variables) leads to unpredictable results, and inconsistent data.

Data never sleeps
Solution
‎03-16-2016 05:42 PM
Super User
Posts: 5,092

Re: How to Merge by Minimum Number of Observations in By Group?

data minimum;

in1=0;

in2=0;

merge a (in=in1) b (in=in2);

by id;

if in1 and in2;

run;

Contributor
Posts: 23

Re: How to Merge by Minimum Number of Observations in By Group?

Perfect. If modified to "if in1" this gives the exact results I needed. Thank you!
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 351 views
  • 0 likes
  • 3 in conversation