Published on :
ETL CREATION_INTERNE

MERGE Operations with IN= Option

This code is also available in: Deutsch Español Français
Awaiting validation
The script begins by creating two temporary datasets, 'empsau' (employees) and 'phonec' (phones), via DATA STEP blocks using DATALINES. Then, several DATA STEP blocks are executed to demonstrate merge scenarios: a full merge (equivalent to a full outer join), a merge to keep only matching records (equivalent to an inner join), and finally merges to isolate records present only in the first dataset, only in the second, or in neither. Each merge operation is followed by a PROC PRINT to display the result.
Data Analysis

Type : CREATION_INTERNE


The 'empsau' and 'phonec' data are created directly within the script via DATA STEP blocks and the DATALINES statement. They are then used and transformed within the same script.

1 Code Block
DATA STEP Data
Explanation :
This DATA STEP block creates the temporary dataset 'empsau' containing employee information (First Name, Gender, Employee ID). The data is provided directly in the script via the DATALINES statement.
Copied!
1DATA empsau;
2 INPUT First $ Gender $ EmpID;
3 DATALINES;
4Togar M 121150
5Kylie F 121151
6Birin M 121152
7;
2 Code Block
DATA STEP Data
Explanation :
This DATA STEP block creates the temporary dataset 'phonec' containing phone numbers associated with Employee IDs. The data is also integrated directly into the script via DATALINES.
Copied!
1DATA phonec;
2 INPUT EmpID Phone $15.;
3 DATALINES;
4121150 +61(2)5555-1795
5121152 +61(2)5555-1667
6121153 +61(2)5555-1348
7;
3 Code Block
DATA STEP
Explanation :
This DATA STEP block performs a full outer join of the 'empsau' and 'phonec' datasets based on the common variable 'EmpID'. The IN= options (Emps, Cell) create temporary boolean variables (_Emps and _Cell) that indicate whether a record originates from 'empsau' or 'phonec' respectively. The PROC PRINT displays all merged records, including those present in only one of the two datasets.
Copied!
1DATA empsauc;
2 MERGE empsau(in=Emps)
3 phonec(in=Cell);
4 BY EmpID;
5RUN;
6 
7PROC PRINT DATA=empsauc;
8RUN;
4 Code Block
DATA STEP
Explanation :
This DATA STEP block performs an inner join. After the merge, the condition 'if Emps=1 and Cell=1;' filters records to keep only those whose Employee ID is present in both 'empsau' and 'phonec'. The PROC PRINT displays only the matches.
Copied!
1DATA empsauc;
2 MERGE empsau(in=Emps)
3 phonec(in=Cell);
4 BY EmpID;
5 IF Emps=1 and Cell=1;
6RUN;
7 
8PROC PRINT DATA=empsauc;
9RUN;
5 Code Block
DATA STEP
Explanation :
This DATA STEP block identifies records that are present in 'empsau' (Emps=1) but absent from 'phonec' (Cell=0). This corresponds to a left anti-join. The PROC PRINT displays employees without a phone match in 'phonec'.
Copied!
1DATA empsauc;
2 MERGE empsau(in=Emps)
3 phonec(in=Cell);
4 BY EmpID;
5 IF Emps=1 and Cell=0;
6RUN;
7 
8PROC PRINT DATA=empsauc;
9RUN;
6 Code Block
DATA STEP
Explanation :
This DATA STEP block identifies records that are present in 'phonec' (Cell=1) but absent from 'empsau' (Emps=0). This corresponds to a right anti-join. The PROC PRINT displays phone numbers without an employee match in 'empsau'.
Copied!
1DATA empsauc;
2 MERGE empsau(in=Emps)
3 phonec(in=Cell);
4 BY EmpID;
5 IF Emps=0 and Cell=1;
6RUN;
7 
8PROC PRINT DATA=empsauc;
9RUN;
7 Code Block
DATA STEP
Explanation :
This DATA STEP block selects all non-matching records. The condition 'if Emps=0 or Cell=0;' filters records that are present only in 'empsau' or only in 'phonec'. The PROC PRINT displays records that do not find a match in the other dataset.
Copied!
1DATA empsauc;
2 MERGE empsau(in=Emps)
3 phonec(in=Cell);
4 BY EmpID;
5 IF Emps=0 or Cell=0;
6RUN;
7 
8PROC PRINT DATA=empsauc;
9RUN;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.