x
Need help studying for the new
SAS Certified Specialist Exam?
SAS Certified Specialist Exam?
Get access to:
- Two Full Certificate Prep Courses
- 300+ Practice Exercises
|
Lesson 4.2: Merging datasets
IMPORTANT! We have recently updated our training materials. Check out the latest free training here. Merging is another data manipulation step that is frequently used in SAS. Example
There are two datasets here: GMAT and ATTEMPT. GMAT contains the students’ GMAT results and ATTEMPT contains the number of attempts for each student. Let’s merge these two datasets. DATA Profile;
MERGE GMAT ATTEMPT; BY STUDENT; RUN; (1) This step indicates which two datasets to be merged.
(2) THIS IS IMPORTANT! This "BY" statement identifies the common variables to merge. It is to ensure the two datasets are merged correctly. In this example, we want the common variable to be STUDENT so that John’s GMAT result will be merged with John’s number of GMAT attempts. You do not want to mix up John’s result with Amy’s attempts. Now, let’s take a look at the log window. There is an error. The datasets are not sorted properly! SAS is not smart enough to sort the datasets prior to merging so we need to do a manual sorting before merging the two datasets.
PROC SORT data=GMAT; By Student; Run; PROC SORT data=ATTEMPT; By Student; Run; Now, we merged them and let’s take another look at the log window.
DATA Profile; MERGE GMAT ATTEMPT; BY STUDENT; RUN; DONE! You have learned how to merge two SAS datasets. |
|