Published on :
Général CREATION_INTERNE

Sans titre

This code is also available in: Deutsch Español Français
Awaiting validation
Data Analysis

Type : CREATION_INTERNE


Examples use generated data (datalines) to ensure code autonomy.

1 Code Block
DATA STEP / PROC PRINT Data
Explanation :
This example illustrates a one-to-one merge of two data sets, 'animal' and 'plantG', which have an equal number of observations. The MERGE statement is used without a BY statement. The values of the 'common' variable in 'plantG' (the last specified data set) overwrite those from 'animal' at the sixth observation, where they differ. Non-common variables are added, and all observations are included in the result.
Copied!
1DATA animal;
2 INPUT common $ animal $;
3 DATALINES;
4a Ant
5b Bird
6c Cat
7d Dog
8e Eagle
9f Frog
10;
11DATA plantG;
12 INPUT common $ plant $;
13 DATALINES;
14a Apple
15b Banana
16c Coconut
17d Dewberry
18e Eggplant
19g Fig
20;
21DATA merged;
22 MERGE animal plantG;
23RUN;
24 
25PROC PRINT DATA=merged; RUN;
2 Code Block
DATA STEP / PROC PRINT Data
Explanation :
This scenario shows a one-to-one merge between 'animal' (6 observations) and 'plantMissing' (3 observations). The MERGE statement without a BY statement will process all records from each data set by combining them line by line. Missing observations in the shorter data set ('plantMissing') result in missing values in the corresponding columns of the resulting data set where there is no implicit match, as the merge continues until all observations from all data sets are processed. If the 'SET' statement had been used, the program would have stopped after reading the last record of the smaller data set.
Copied!
1DATA animal;
2INPUT common $ animal $;
3DATALINES;
4a Ant
5b Bird
6c Cat
7d Dog
8e Eagle
9f Frog
10;
11 
12DATA plantMissing;
13INPUT common $ plant $;
14DATALINES;
15a Apple
16b Banana
17c Coconut
18;
19 
20DATA merged;
21 MERGE animal plantmissing;
22RUN;
23PROC PRINT DATA=merged; RUN;
3 Code Block
DATA STEP / PROC PRINT Data
Explanation :
This example illustrates the potentially undesirable results of a one-to-one merge without a BY statement when data sets ('animalDupes', 'plantDupes') contain duplicate values for the 'common' variable. One-to-one merges are designed for data with a one-to-one relationship. In this scenario (one-to-many or many-to-one relationships), the merge occurs line by line, overwriting common variable values and potentially leading to loss of information or incorrect combination of observations.
Copied!
1DATA animalDupes;
2INPUT common $ animal $;
3DATALINES;
4a Ant
5a Ape
6b Bird
7c Cat
8d Dog
9e Eagle
10;
11 
12DATA plantDupes;
13INPUT common $ plant $;
14DATALINES;
15a Apple
16b Banana
17c Coconut
18c Celery
19d Dewberry
20e Eggplant
21;
22 
23DATA merged;
24 MERGE animalDupes plantDupes;
25RUN;
26PROC PRINT DATA=merged; RUN;
4 Code Block
DATA STEP / PROC PRINT Data
Explanation :
This example demonstrates the undesirable results of a one-to-one merge without a BY statement when data sets ('animalMissing', 'plantMissing2') have different values for their common variable 'common'. The merge occurs line by line, and values from 'plantMissing2' overwrite those from 'animalMissing' for the 'common' variable. Without explicit value matching via a BY statement, the data combination can be incorrect and may not reflect the expected relationships between observations.
Copied!
1DATA animalMissing;
2 INPUT common $ animal $;
3 DATALINES;
4a Ant
5c Cat
6d Dog
7e Eagle
8;
9DATA plantMissing2;
10 INPUT common $ plant $;
11 DATALINES;
12a Apple
13b Banana
14c Coconut
15e Eggplant
16f Fig
17;
18DATA merged;
19 MERGE animalMissing plantMissing2;
20RUN;
21PROC PRINT DATA=merged; 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.
Copyright Info : Copyright © SAS Institute Inc. All Rights Reserved