DATA Step, Macro, Functions and more

using modify with duplicate key values in master data set and transaction data set

Accepted Solution Solved
Reply
Contributor
Posts: 44
Accepted Solution

using modify with duplicate key values in master data set and transaction data set

Hi,

   As my data set like: I get the code,but I cannot understand it and i want to know if there exists other methods. Thank you!

     

 

data master(index=(ssn));
input ssn: $11. nickname $;
datalines;
161-60-5881 joshua
161-60-5881 joshua
160-58-1223 kathryn
160-58-1223 kathryn
134-56-9094 megan
;
data trans;
input ssn : $11. nickname $;
datalines;
161-60-5881 josh
160-58-1223 kathy
160-58-1223 kate
134-56-9094 meg
142-67-9888 bill
;

proc sort data = trans;
by ssn;
data master;
set trans;
by ssn;
dummy = 0;
do until (_iorc_=%sysrc(_dsenom));
if dummy then ssn = '999-99-9999';
modify master key = ssn;
select(_iorc_);
when (%sysrc(_sok)) do;
nickname = tnickname;
replace master;
end;
when (%sysrc(_dsenom)) do;
_error_=0;
if not last.ssn and not dummmy then do;
dummy = 1;
_iorc_=0;
end;
end;
otherwise;
end;
end;

 


Accepted Solutions
Solution
‎01-21-2017 09:15 AM
Super Contributor
Posts: 408

Re: using modify with duplicate key values in master data set and transaction data set

It is in the nature of the MODIFY statement with KEY= to retrieve the rows from the master in order of the index, starting with the first row. If you repeatedly retrieve rows with the same KEY lookup value until the last record is found. At that moment  the macro %SYSRC yields a &_DSENOM signalling a non-match. Note that the MODIFY statement actually retrieves rows much like SET and does not in itself updates in the specified table. Other than that please make clear what you do not understand so we can address that mor eeffectively.

 

An alternative is to use SQL using UPDATE with a SELECT subquery:

 

proc sql;

update master

set nickname=(select nickname from trans where mastern.nickname=trans.nickname)

;

run;

Other advanced methods exist like using a hastable. I leave that as homework.

 

<soapbox>In general ( I know we're looking an example from the SAS docs) I object to master tables with duplicate keys. They can occur but only when also a primary (unique) key exists.</soapbox>

 

Hope this helps,

-- Jan

View solution in original post


All Replies
Solution
‎01-21-2017 09:15 AM
Super Contributor
Posts: 408

Re: using modify with duplicate key values in master data set and transaction data set

It is in the nature of the MODIFY statement with KEY= to retrieve the rows from the master in order of the index, starting with the first row. If you repeatedly retrieve rows with the same KEY lookup value until the last record is found. At that moment  the macro %SYSRC yields a &_DSENOM signalling a non-match. Note that the MODIFY statement actually retrieves rows much like SET and does not in itself updates in the specified table. Other than that please make clear what you do not understand so we can address that mor eeffectively.

 

An alternative is to use SQL using UPDATE with a SELECT subquery:

 

proc sql;

update master

set nickname=(select nickname from trans where mastern.nickname=trans.nickname)

;

run;

Other advanced methods exist like using a hastable. I leave that as homework.

 

<soapbox>In general ( I know we're looking an example from the SAS docs) I object to master tables with duplicate keys. They can occur but only when also a primary (unique) key exists.</soapbox>

 

Hope this helps,

-- Jan

Contributor
Posts: 44

Re: using modify with duplicate key values in master data set and transaction data set

Thank you! i am wondering that why it used the 'dummy variable'  and as for the hash table.i will try to program with it.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 201 views
  • 0 likes
  • 2 in conversation