Sample 24682: Combining multiple data sets with differing attributes for the BY variable
Merge data sets with BY variables with different lengths or types.
Use the format associated with a common variable from the second
data set rather than the first data set.
Note:
All BY variables must be in all data sets and of the same type.
If they are not the same type use either the PUT function to
convert the numeric version to character, or the INPUT function
to convert the character version to numeric.
If the BY variables have different attributes such as LENGTH or
FORMAT, the attributes from the data set named first on the MERGE
statement will be used. To override, use an ATTRIB statement
prior to the MERGE statement.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
/*********************************************************************************/
/* Note the type and lengths of the BY variables is not consistent for all */
/* three data sets. */
/* */
/* In the first data set the Zw.d format is associated with SALE. In the second */
/* data set SALE used the DOLLARw.d format. Use and ATTRIB statement to */
/* associate the DOLLAR7.2 format with SALE in the final data set. */
/*********************************************************************************/
data sample1;
input id $5. team $ sale;
format sale z5.2;
datalines;
00345 red 9.15
01234 blue 4.75
55555 green 10.38
;
data sample2;
input id :$7. team $ sale flag :$3.;
format sale dollar7.2;
datalines;
00345 red 8.15 -1
01234 blue 5.75 +1
10000 yellow 5.00 +0
55555 green 11.38 +1
;
data sample3;
input id num;
datalines;
00345 1
01234 3
55555 2
;
/* SAMPLE3 has the wrong type for ID. Change the type from numeric to character */
/* prior to the MERGE. Use the RENAME= option to allow the original variable */
/* name to be used as the new variable name in the type conversion. DROP= */
/* keeps the old numeric version of ID from being added to NEW_SAMPLE3. */
data new_sample3 (drop=numid);
set sample3 (rename=(id=numid));
id=put(numid,z5.-l);
run;
data final;
attrib id length=$5
sale format=dollar7.2;
merge sample1 sample2 new_sample3;
by id;
run;
proc print data=final;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Obs id sale team flag num
1 00345 $8.15 red -1 1
2 01234 $5.75 blue +1 3
3 10000 $5.00 yellow +0 .
4 55555 $11.38 green +1 2
Merge data sets with BY variables with different lengths or types.
Use the format associated with a common variable from the second
data set rather than the first data set.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step SAS Reference ==> Statements ==> File-handling ==> MERGE Common Programming Tasks ==> Combining Data SAS Reference ==> Statements ==> File-handling ==> MERGE ==> with BY
|
| Date Modified: | 2008-01-28 11:09:19 |
| Date Created: | 2004-09-30 14:09:03 |
Operating System and Release Information
| SAS System | Base SAS | All | n/a | n/a |