Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- SAS Procedures
- /
- How to Merge by Minimum Number of Observations in By Group?

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 03-16-2016 04:26 PM
(960 views)

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.

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

data minimum;

in1=0;

in2=0;

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

by id;

if in1 and in2;

run;

5 REPLIES 5

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

data minimum;

in1=0;

in2=0;

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

by id;

if in1 and in2;

run;

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Perfect. If modified to "if in1" this gives the exact results I needed. Thank you!

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

**If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. **

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.