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
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?
Hi,
PaigeMiller
Using Merge with by variable second dataset overwrite first dataset
why its happend
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:
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.
Hi:
Here are some slides I did in a presentation to illustrate SET vs MERGE
SET:
Simple MERGE
all possible MERGE outputs:
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.