MailLib Concept - Exercise #12

with solution

Topic

Avoiding an implicit retain while merging two data sets in a data step

Keywords

*data step #merge

Data

data one;
    subject=1; test='A'; x=1; output;
    subject=1; test='B'; x=2; output;
    subject=1; test='C'; x=3; output;
    subject=2; test='A'; x=1; output;
run; 
data two;
    subject=1; y=100; output;
    subject=2; y=200; output;
run; 
Dataset one Dataset two

Program

data demo;
    merge one two;
    by subject;
    if test='B' then y=999;
run; 
Dataset one Dataset two Dataset demo

The datasets one and two are merged by subject. The value of y is then updated when test='B'. But When test='C', the value of y does not remain 100; It is also modified.

Task

Update the program to get the following result:

Dataset one Dataset two Dataset demo (Updated)

Possible Solution

data demo;
    merge one two;
    by subject;
run;
 
data demo;
    set demo;
    if test='B' then y=999;
run; 

Resource Hub

Module 5 Data Handling 3
Module 5, Chapter 2 Merging Several Datasets
Module 5, Chapter 2.8 Example 16: Avoiding an Implicit retain

Copyright © 2024 xxformat GmbH, All Rights Reserved

xxformat GmbH

c/o STARTPLATZ, Im Mediapark 5
50670 Cologne
Germany

Amtsgericht Köln HRB 90276

Geschäftsführer: Véronique Bourcier
veronique.bourcier@xxformat.com
+49 1577 288 6066