BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
JNWong
Calcite | Level 5

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
jklaverstijn
Rhodochrosite | Level 12

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

2 REPLIES 2
jklaverstijn
Rhodochrosite | Level 12

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

JNWong
Calcite | Level 5

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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