BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
john_floralde
Fluorite | Level 6

We have the following code where we perform a table lookup using set statement with key = option.

 

*master table;
data ftr; trnid = 'trade1';
trnidsce = 'a';
issueid = '';
issueidsce = '';
rcvudlyid = 'ABC';
rcvudlyidsce = '2'; run;

*lookup table;
data ins (index = (issueKey = (issueid issueidsce) / unique)) ;
issueid = 'ABC';
issueidsce = '2';
 ciultarlvcflg = 'Y';
run;

*final table;
data final;
 set ftr;
  set ins2 (keep = issueid issueidsce ciultarlvcflg 
           rename = (issueid = rcvudlyid
                     issueidsce = rcvudlyidsce)) key = issueKey / unique;
  if _iorc_ then _error_ = 0;
run;

As I understand the key = option should do the lookup via definition of issueKey, i.e. if issueid and issueidsce is matching between ins2 and ftr dataset. However it seems the lookup is happening by rcvudlyid rcvudlyidsce after the rename and I am now able to get value of ciultarlvcflg. I was expecting it to throw an error given the original variables used to create the index was renamed. Can someone help me to understand what is happening? Many thanks.

 

john_floralde_0-1659342836018.png

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @john_floralde,

 

The KEEP= option is (always) applied before the RENAME= option.

 

If the key variables in dataset INS were renamed permanently with PROC DATASETS, the index would be modified correspondingly (see PROC CONTENTS output), thus maintaining its integrity. Apparently, SAS handles a temporary renaming via dataset options in a similar way: temporarily the index works with the new names. (I assume that the "2" in ins2 in your code was not intended.)

 

This is a useful feature because it allows you to create indexed lookup tables without worrying about variable names in future "master" tables. Your example is a case in point, as simply renaming the RCVU... key variables of dataset FTR is not an option because of the existing ISSUE... variables. So, one lookup table can be used for many master tables, regardless of their key variable names.

View solution in original post

2 REPLIES 2
FreelanceReinh
Jade | Level 19

Hello @john_floralde,

 

The KEEP= option is (always) applied before the RENAME= option.

 

If the key variables in dataset INS were renamed permanently with PROC DATASETS, the index would be modified correspondingly (see PROC CONTENTS output), thus maintaining its integrity. Apparently, SAS handles a temporary renaming via dataset options in a similar way: temporarily the index works with the new names. (I assume that the "2" in ins2 in your code was not intended.)

 

This is a useful feature because it allows you to create indexed lookup tables without worrying about variable names in future "master" tables. Your example is a case in point, as simply renaming the RCVU... key variables of dataset FTR is not an option because of the existing ISSUE... variables. So, one lookup table can be used for many master tables, regardless of their key variable names.

john_floralde
Fluorite | Level 6

Interesting. Thank you very much for your explanation! And yeah you are write I miswrote ins2 instead of ins when doing the lookup in the code.

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
  • 479 views
  • 1 like
  • 2 in conversation