MERGE Statement
Joins observations from two or more SAS data sets into a single observation.
| Valid in: | DATA step |
| Categories: | CAS |
| File-Handling | |
| Type: | Executable |
| Note: | The variables that are read using the MERGE statement are retained in the PDV. The data types of the variables that are read are also retained. RETAIN Statement. |
Syntax
SAS-data-set-2 <(data-set-options) >
<...SAS-data-set-n<(data-set-options)> >
<END=variable>;
Arguments
SAS-data-set
specifies at least two existing SAS data sets from which observations are read. You can specify individual data sets, data set lists, or a combination of both.
| Tips | Instead of using a data set name, you can specify the physical pathname to the file, using syntax that your operating system understands. The pathname must be enclosed in single or double quotation marks. |
| You can specify additional SAS data sets. | |
| See | Using Data Set Lists with MERGE |
(data-set-options)
specifies one or more SAS data set options in parentheses after a SAS data set name.
| Note | The data set options specify actions that SAS is to take when it reads observations into the DATA step for processing. For a list of data set options, see the SAS Viya Data Set Options: Reference |
| Tip | Data set options that apply to a data set list apply to all of the data sets in the list. |
END=variable
names and creates a temporary variable that contains an end-of-file indicator.
| Note | The variable, which is initialized to 0, is set to 1 when the MERGE statement processes the last observation. If the input data sets have different numbers of observations, the END= variable is set to 1 when MERGE processes the last observation from all data sets. |
| Tip | The END= variable is not added to any SAS data set that is being created. |
Details
Overview
Using Data Set Lists with MERGE
merge
SALES1:; tells SAS to merge all data sets starting with
"SALES1" such as SALES1, SALES10, SALES11, and SALES12.
sales1 sales2 sales3 sales4 sales1-sales4
-
You can specify groups of ranges.
merge cost1-cost4 cost11-cost14 cost21-cost24;
-
You can mix numbered range lists with name prefix lists.
merge cost1-cost4 cost2: cost33-37;
-
You can mix single data sets with data set lists.
merge cost1 cost10-cost20 cost30;
-
Quotation marks around data set lists are ignored.
/* these two lines are the same */ merge sales1-sales4; merge 'sales1'n-'sales4'n;
-
Spaces in data set names are invalid. If quotation marks are used, trailing blanks are ignored.
/* blanks in these statements will cause errors */ merge sales 1-sales 4; merge 'sales 1'n - 'sales 4'n; /* trailing blanks in this statement will be ignored */ merge 'sales1'n - 'sales4'n;
-
The maximum numeric suffix is 2147483647.
/* this suffix will cause an error */ merge prod2000000000-prod2934850239;
-
Physical pathnames are not allowed.
/* physical pathnames will cause an error */ %let work_path = %sysfunc(pathname(WORK)); merge "&work_path\dept.sas7bdat"-"&work_path\emp.sas7bdat" ;
One-to-One Merging
Match-Merging
Comparisons
-
MERGE combines observations from two or more SAS data sets. UPDATE combines observations from exactly two SAS data sets. UPDATE changes or updates the values of selected observations in a master data set as well. UPDATE also might add observations.
-
Like UPDATE, MODIFY combines observations from two SAS data sets by changing or updating values of selected observations in a master data set.
-
The results that are obtained by reading observations using two or more SET statements are similar to the results that are obtained by using the MERGE statement with no BY statement. However, with the SET statements, SAS stops processing before all observations are read from all data sets if the number of observations are not equal. In contrast, SAS continues processing all observations in all data sets named in the MERGE statement.
Examples
Example 1: One-to-One Merging
data benefits.qtr1; merge benefits.jan benefits.feb; run;
Example 2: Match-Merging
data inventry; merge stock orders; by partnum; run;
Example 3: Merging with a Data Set List
data d008; job=3; emp=19; run; data d009; job=3; sal=50; run; data d010; job=4; emp=97; run; data d011; job=4; sal=15; run; data comb; merge d008-d011; by job; run; proc print data=comb; run;
Example 4: Three Table Merge with BY Values and the IN= Data Set Option
DATA CAFE(KEEP=NAME PLACE CNUM);
INPUT NAME $ ;
PLACE = 'CAFE ';
CNUM = 'C' || LEFT(PUT(_N_,2.));
DATALINES;
ANDERSON
COOPER
DIXON
FREDERIC
FREDERIC
PALMER
RANDALL
RANDALL
SMITH
SMITH
SMITH
;
RUN;
DATA VENDING (KEEP=NAME PLACE VNUM);
INPUT NAME $ ;
PLACE = 'VENDING ';
VNUM = 'V' || LEFT(PUT(_N_,2.));
DATALINES;
CARTER
DANIELS
GARY
GARY
HODGE
PALMER
RANDALL
RANDALL
SMITH
SMITH
SPENCER
SPENCER
SPENCER
SPENCER
;
RUN;
DATA SNACK (KEEP=NAME PLACE SNUM);
INPUT NAME $ ;
PLACE = 'SNACK ';
SNUM = 'S' || LEFT(PUT(_N_,2.));
DATALINES;
BARRETT
COOPER
DANIELS
DIXON
DIXON
FREDERIC
GARY
HODGE
HODGE
PALMER
RANDALL
RANDALL
SMITH
SMITH
SMITH
SMITH
SPENCER
SPENCER
;
RUN;
DATA ALL;
MERGE CAFE(IN=CAFEIN) SNACK(IN=SNACKIN) VENDING(IN=VENDIN);
BY NAME;
CIN=CAFEIN; SIN=SNACKIN; VIN=VENDIN;
RUN;
PROC PRINT;
TITLE 'MERGED DATA';
RUN;
Example 5: Two Table Merge with a BY Variable and the IN= Data Set Option
data have_a; input ID amount_a; datalines; 1 10 3 15 4 20 7 15 9 12 10 14 ; data have_b; input ID amount_b; datalines; 2 15 3 20 4 10 5 12 7 20 8 15 9 10 11 20 ; data want; merge have_a(in=inA) have_b(in=inb); by id; length joinType $ 2; joinType = cats(inA, inB); run; proc print data=want; run; quit;
