In SAS, how do I merge two data sets?
To merge two or more data sets in SAS, you must first sort
both data sets by a shared variable upon which the merging will be
based. Then use the MERGE statement in your
DATA statement.
If you merge data sets without sorting, called one-to-one merging, data of the merged file overwrites the primary data set without considering whether or not two observations are the same.
For example, suppose you have two data sets (one and
two below), with a common variable, id. The
SAS codes below show how they can be sorted and merged:
In the example above, data set three is created by
merging data sets one and two. It will have
five variables (id, and v1 to
v4) and four cases. Where id=4, variables
v1 and v2 will be missing.
For more information about statistical and mathematical software, email the UITS Stat/Math Center, visit the center's web page, or phone 812-855-4724 (IUB) or 317-278-4740 (IUPUI). The center is located in Bloomington at 410 N. Park Avenue, and is open for consultation by appointment Monday-Friday 9am-5pm.
Also see:
- During a SAS session, how do I redirect the work files?
- In SAS, how can I randomly assign half the cases to one group and the remaining half to another?
- In SAS, how do I convert data sets to a format LIMDEP can use?
- In SAS, how do I create a permanent SAS data file?
- In SAS, how do I access a permanent data set stored on a disk?
Last modified on November 14, 2007.






