Sample 24681: Merge data sets that have an uneven number of members in the BY groups and reset the remaining observations to "missing"
Use the IN= option to determine whether both data sets contribute to the BY group. If not, reset specific values to missing to stop values from carrying down the BY group.
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.
/***********************************************************************/
/* It is the nature of MERGE to carry values of unique variables */
/* down the BY-Group. If you do not want this result, you have */
/* to add additional coding logic. */
/* */
/* In a DATA step, the IN= variables reset when the BY-Group changes. */
/* If you reset these IN= variables prior to the MERGE statement, */
/* you can conditionally check to see if an observation is contributed */
/* from a BY-Group with each iteration of the DATA step. If there are */
/* remaining members of a BY-Group in one data set, set the variables */
/* to missing (excluding the BY variables). */
/* */
/* Starting Data: ---> Desired Results: */
/* */
/* DATA A DATA B DATA NEW */
/* */
/* ID X ID Y ID X Y */
/* 1 11 1 111 1 11 111 */
/* 1 22 1 222 1 22 222 */
/* 1 33 1 33 . */
/* 1 44 1 44 . */
/* 1 55 1 55 . */
/***********************************************************************/
data a;
input id x;
datalines;
1 11
1 22
1 33
1 44
1 55
;
data b;
input id y;
datalines;
1 111
1 222
;
/* Reset IN= variables to 'false', or zero. If an */
/* observation contributes from A and not B, then */
/* reset Y to missing */
data new;
ina=0;
inb=0;
merge a(in=ina) b(in=inb);
by id;
if ina and not inb then y=' ';
run;
proc print data=new;
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 x y
1 1 11 111
2 1 22 222
3 1 33 .
4 1 44 .
5 1 55 .
Use the IN= option to determine whether both data sets contribute to the BY group. If not, reset specific values to "missing" to stop values from carrying down the BY group.
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 IN=
|
Date Modified: | 2008-01-28 11:08:42 |
Date Created: | 2004-09-30 14:09:03 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |