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

Hi,

When I am merging 2 datasets- A and B with 247 and 18 observations respectively as below , I am getting 248 observations in the dataset AB. Is this correct?

As much as I know, the no.of observations in the new dataset AB should be 247. Need help in solving this problem. Appreciate it.

data AB AB_dummy;

length common_variable $ 50;

merge A (in=a) B (in=b);

by common_variable;

if a then output AB;

if a and not b then output AB_dummy;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

One scenario is there are more group value in B than in A.

For example :

data a;
group=1;v=2; output;
group=2;v=2; output;
run;
data b;
group=1;w=2; output;
group=2;w=2; output;
group=2;w=2; output;
run;

data ab;
merge A (in=a) B (in=b);
by group;
if a ;
run;

if you strictly request the same number of obs with A , use this :

data ab;
a=0;
merge A (in=a) B (in=b);
by group;
if a ;
run;

Xia Keshan

View solution in original post

3 REPLIES 3
Ksharp
Super User

One scenario is there are more group value in B than in A.

For example :

data a;
group=1;v=2; output;
group=2;v=2; output;
run;
data b;
group=1;w=2; output;
group=2;w=2; output;
group=2;w=2; output;
run;

data ab;
merge A (in=a) B (in=b);
by group;
if a ;
run;

if you strictly request the same number of obs with A , use this :

data ab;
a=0;
merge A (in=a) B (in=b);
by group;
if a ;
run;

Xia Keshan

pavan_reddy
Calcite | Level 5

Thanks a lot Xia, this helped me a lot

Tom
Super User Tom
Super User

Your questions can be rephrased as how can there be MORE observations output to AB than read from A when you only output to AB when the record was in A?

This can occur on a merge when one of the other datasets in the merge has more observations for the same by group.

Consider a simple example.  If A has 4 observations with GROUP values of (1 2 3 4) and B has 3 observations when GROUP values of (1 1 2 ) then your program will generate 5 observations to AB because GROUP 1 will have two observations.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 657 views
  • 4 likes
  • 3 in conversation