BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10
data m2m;
infile datalines;
input id$ x;
cards;
A25 24
A25 22
A25 76
;
run;

data m2m1;
infile datalines;
input id$ x;
cards;
A25 24
A25 22
A25 6
;
run;

data m2m3;
merge m2m m2m1;
proc print;
run;/*with out by variable*/

proc sort data =m2m1;
by id;
run;

proc sort data=m2m1;
by id;
run;


data m2m3;
merge m2m m2m1;
by id;
proc print;
run;/*with by variable*/

proc sort data =m2m1;
by id;
run;

proc sort data=m2m1;
by id;
run;

data s2s;
set m2m m2m1;
by id;
proc print;
run;

Here i using set and Merge statements  in set with by variable all obeservations came but merge with by variable second dataset over write in first dataset 

5 REPLIES 5
PaigeMiller
Diamond | Level 26

Here i using set and Merge statements  in set with by variable all obeservations came but merge with by variable second dataset over write in first dataset 

Yes, this is expected when you merge data sets with the exact same variable name in both data sets. What is your question?

--
Paige Miller
BrahmanandaRao
Lapis Lazuli | Level 10

Hi,

PaigeMiller

 

Using Merge with by variable second dataset overwrite first dataset 

why its happend 

 

Kurt_Bremser
Super User

Since all variables appear in both datasets, the PDV (Program Data Vector) will have two variables, id and x.

Since both datasets contain an equal number of observations with identical ud variables, both the MERGE with and without BY will have the same results:

  • an observation from m2m is read, and its contents put into the PDV
  • an observation from m2m1 is read, its contents overwriting the values in the PDV
  • these values are written to the output

When SET with a BY is used, the data step will interleave the datasets, it reads one observation for an id value from m2m, writes it to the output, reads an observation for the same id from m2m1, and writes that. This is why you have 3 observations after the MERGE, but 6 after the SET.

PaigeMiller
Diamond | Level 26

@BrahmanandaRao 

 

What result would you like to see instead of overwriting the data?

--
Paige Miller
Cynthia_sas
SAS Super FREQ

Hi:

  Here are some slides I did in a presentation to illustrate SET vs MERGE

 

SET:

Cynthia_sas_0-1600354584545.png

 

 

Simple MERGE

Cynthia_sas_1-1600354626024.png

all possible MERGE outputs:

Cynthia_sas_2-1600354731399.png

 

Cynthia_sas_4-1600354789787.png

 

 

 

I recommend nearly ALWAYS using a BY and IN= with MERGE to make sure you have complete control over the MERGE output.

 

Cynthia

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 983 views
  • 0 likes
  • 4 in conversation