If your reference table doesn't have millions of records, in can all be done in a single sql query as follow proc sql; create table want as select a.city1, a.city2, (case when a.city2 in (select distinct city_a from reftable) then city2 when (a.city2 not in (select distinct city_a from reftable) and a.city1 in (select distinct city_a from reftable)) then city1 else "ROC" end) as city_A from have; quit; Above would be the code with the best readability but if the (select distinct city_a from reftable) returns hundred of thousands of records, then the "in" operator lags significantly behind other processing strategies. Typically, when I want to do some funky merge between a large DS and a smaller one, as you've described above, I tend to use hash tables. Here's an alternative solution, again in a single step with significantly faster processing but using the less natural hash object syntax data want(rename=(city_c=city_a)); if _N_=1 then do; length city_a $40.; /* placeholder length, preferably use the same as the length of your city_a variable in your reference table */ declare hash h(datasets: 'reftable'); h.defineKey('city_a'); h.defineData(); /* only necessary if you ought to retain additional columns from reference table in the merge */ h.defineDone(); /* I have a blank this may or may not require the () for appropriate syntax */ end; set have; if h.find(key: city2)=0 then city_c=city2; else if h.find(key:city1)=0 then city_c=city1; else city_c="ROC"; run; Basically, hash table is a lookup table. RC is the return code of the find method on the hash table. It is 0 if the key was found, some code NE 0 otherwise. Hash table is all in memory which means great gains of efficiency for medium-large datasets. However, it is capped the RAM allocation of your computer to SAS. With a single key variable and no data you should have no problem get a hash table of 1M records though. Vince
... View more