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!
data empsau;
input First $ Gender $ EmpID;
datalines;
Togar M 121150
Kylie F 121151
Birin M 121152
;
1
DATA empsau;
2
INPUT First $ Gender $ EmpID;
3
DATALINES;
4
Togar M 121150
5
Kylie F 121151
6
Birin 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.
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!
data empsauc;
merge empsau(in=Emps)
phonec(in=Cell);
by EmpID;
run;
proc print data=empsauc;
run;
1
DATA empsauc;
2
MERGE empsau(in=Emps)
3
phonec(in=Cell);
4
BY EmpID;
5
RUN;
6
7
PROC PRINTDATA=empsauc;
8
RUN;
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!
data empsauc;
merge empsau(in=Emps)
phonec(in=Cell);
by EmpID;
if Emps=1 and Cell=1;
run;
proc print data=empsauc;
run;
1
DATA empsauc;
2
MERGE empsau(in=Emps)
3
phonec(in=Cell);
4
BY EmpID;
5
IF Emps=1 and Cell=1;
6
RUN;
7
8
PROC PRINTDATA=empsauc;
9
RUN;
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!
data empsauc;
merge empsau(in=Emps)
phonec(in=Cell);
by EmpID;
if Emps=1 and Cell=0;
run;
proc print data=empsauc;
run;
1
DATA empsauc;
2
MERGE empsau(in=Emps)
3
phonec(in=Cell);
4
BY EmpID;
5
IF Emps=1 and Cell=0;
6
RUN;
7
8
PROC PRINTDATA=empsauc;
9
RUN;
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!
data empsauc;
merge empsau(in=Emps)
phonec(in=Cell);
by EmpID;
if Emps=0 and Cell=1;
run;
proc print data=empsauc;
run;
1
DATA empsauc;
2
MERGE empsau(in=Emps)
3
phonec(in=Cell);
4
BY EmpID;
5
IF Emps=0 and Cell=1;
6
RUN;
7
8
PROC PRINTDATA=empsauc;
9
RUN;
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!
data empsauc;
merge empsau(in=Emps)
phonec(in=Cell);
by EmpID;
if Emps=0 or Cell=0;
run;
proc print data=empsauc;
run;
1
DATA empsauc;
2
MERGE empsau(in=Emps)
3
phonec(in=Cell);
4
BY EmpID;
5
IF Emps=0 or Cell=0;
6
RUN;
7
8
PROC PRINTDATA=empsauc;
9
RUN;
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.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.