How can I perform a one-to-many merge in SAS? 2

How can I perform a one-to-many merge in SAS?

A one-to-many merge in SAS is a data manipulation technique used to combine two datasets based on a one-to-many relationship between a common variable. This allows for the creation of a single merged dataset containing information from both datasets, with repeated values from one dataset matched to corresponding values in the other dataset. This merge can be performed by using the MERGE statement in the DATA step or by using the PROC SQL procedure in SAS. The resulting merged dataset can be used for further analysis and reporting purposes.

Perform One-to-Many Merge in SAS


You can use the following syntax to perform a one-to-many merge in SAS:

data final_data;
  merge data_one data_many;
  by ID;
run;

This particular example creates a new dataset called final_data by merging the datasets called data_one and data_many on the variable called ID.

In the data_one dataset, each unique ID value only appears once.

In the data_many dataset, each unique ID value occurs multiple times.

This is known as a one-to-many merge.

The following example shows how to use this syntax in practice.

Example: One-to-Many Merge in SAS

Suppose we have the following dataset called data_one that contains information about sales personnel at some company:

/*create dataset*/
data data_one;
    input ID Gender $;
    datalines;
1 Male
2 Male
3 Female
4 Male
5 Female
;
run;

/*view dataset*/ proc printdata = data_one;

Notice that each unique ID value only occurs once in the dataset.

Now suppose we have another dataset called data_many that contains information about sales made by each sales person at various locations:

/*create dataset*/
data data_many;
    input ID Store $ Sales;
    datalines;
1 A 22
1 B 25
1 C 20
2 A 14
2 B 23
3 A 10
4 A 15
4 B 29
5 A 16
5 C 22
;
run;

/*view dataset*/ proc printdata = data_many;

Notice that each unique ID value occurs multiple times.

We can use the following syntax to perform a one-to-many merge using these datasets:

/*create new dataset using one-to-many merge*/
data final_data;
  merge data_one data_many;
  by ID;
run;

/*view new dataset*/
proc printdata=final_data;

The one-to-many merge produced a new dataset that contains all information from both datasets.

Note: You can find the complete documentation for the SAS merge statement .

The following tutorials explain how to perform other common tasks in SAS:

Cite this article

stats writer (2024). How can I perform a one-to-many merge in SAS?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-perform-a-one-to-many-merge-in-sas/

stats writer. "How can I perform a one-to-many merge in SAS?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-perform-a-one-to-many-merge-in-sas/.

stats writer. "How can I perform a one-to-many merge in SAS?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-perform-a-one-to-many-merge-in-sas/.

stats writer (2024) 'How can I perform a one-to-many merge in SAS?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-perform-a-one-to-many-merge-in-sas/.

[1] stats writer, "How can I perform a one-to-many merge in SAS?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I perform a one-to-many merge in SAS?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top