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

Hello,

I'll truly appreciate if somebody helps me out.

I wonder why I get a data including duplicate rows after merging.

My code is following.

proc sort data=work.A;                /**table A has 500 rows**/

     by store product week;

proc sort data=work.B;                /**table B has 800 rows**/

     by store product week;

data work.A;

     merge work.A (in=in1) work.B (in=in2 keep=store product week advertise);

     by store product week;

     if in1;

run;


This code is merging table A and B in order to add "advertise" column in the table A.

I expected the number of table A's rows is 500 since the code has "if in1;". However, it was 800. Does anyone know why?


Jason

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

You get an observation for each matching obs. As long as every obs in work.B has a match in work.A, they will be included in the output set (because the condition if in1 is always met)

If you do not want multiple observations, you need to eliminate the duplicates in Work.B first. Or include a first. or last. condition.

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

You get an observation for each matching obs. As long as every obs in work.B has a match in work.A, they will be included in the output set (because the condition if in1 is always met)

If you do not want multiple observations, you need to eliminate the duplicates in Work.B first. Or include a first. or last. condition.

damanaulakh88
Obsidian | Level 7


Hi Sarang,

You need to remove the duplicates using "nodupkey" option while you sort,  this would eliminate your issue.

You can try the below code example:

data one;
input store $ product $ week;
cards;
1 a 23
2 b 45
3 a 21
1 a 34
1 a 23
2 b 45
;
run;

data two;
input store $ product $ week advertise $;
cards;
1 a 23 y
2 b 45 y
1 a 34 n
2 b 45 y
3 a 21 y
5 c 45 y
7 d 76 y
;
run;


proc sort data=one nodupkey;
by store product week;
run;

proc sort data=two nodupkey;
by store product week;
run;

data final;
merge one(in=a) two(in=b keep=store product week advertise);
by store product week;
if a;
run;

Output:

Capture.JPG

Hope this helps!!

Thanks,

Daman

TomKari
Onyx | Level 15

Run this, and take a look at the results. I think this will clarify how the MERGE works.

data a;

input week;

cards;

1

2

3

4

run;

data b;

input week advertise;

cards;

1 9

2 9

2 9

2 9

3 9

4 9

run;

data c;

merge a(in=in1) b;

  by week;

if in1;

run;

sarang
Fluorite | Level 6

I really appreciate your answers!

They clarified my misunderstanding of merging.

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
  • 4 replies
  • 18910 views
  • 9 likes
  • 4 in conversation