Q: Build and use incremental hash variable in SAS I am trying to create a hash variable that is built incrementally. The specific problem I am trying to solve is that I have a column of currency pairs:
|--------------------|
| ID | CurrencyPair  |
|----|---------------|
| 1  | USD/GBP       |
| 2  | GBP/USD       |
| 3  | USD/BRL       |
| ...| ...           |

I want currency pair for row 1 and currency pair for row 2 (USD/GBP) and (GBP/USD) to be recognized as the same. So I am trying to implement the following algorithm:


*

*Create an empty column CurrencyPairRecode

*Create a hash variable declare hash h(); h.defineKey('k'); h.defineData('d');

*For every row of data, lookup if the currency pair exists in the hash table. If it does the value of CurrencyPairRecode is the same as CurrencyPair
rc = h.Check(key: CurrencyPair)
IF (rc=0) THEN
    CurrencyPairRecode =  CurrencyPair

*If not, check if the flipped currency pair is in the hash table. If it is, CurrencyPairRecode is the flipped value
CALL CATX("/",FLIPPED,SUBSTR(SETTLEMENT_EXCHANGE_RATE_BASIS, 4, 3),SUBSTR(SETTLEMENT_EXCHANGE_RATE_BASIS, 1, 3));
flip_rc = h.Check(key: FLIPPED);
IF (flip_rc = 0) THEN
    CurrencyPairRecode = flipped;

*If neither, CurrencyPairRecode is the same as CurrencyPair and add CurrencyPair to the hash table.
IF (rc^=0 AND flip_rc^= 0) THEN
    h.ADD(key: CurrencyPair, data: 1);
    CurrencyPairRecode = CurrencyPair
I have tried this code but am getting errors. I am completely new to SAS so not sure how to troubleshoot. All help is appreciated.

A: The general approach I'd use is to store the currency pair in always sorted order.  This is particularly appealing when the order is really not relevant (as you don't have to keep track of it).
I would do something like this.
data have;
  input ID  CurrencyPair $;
  datalines;
1  USD/GBP
2  GBP/USD
3  USD/BRL
;;;;
run;

data for_hash;
  set have;
  array curs[2] $ _temporary_;
  curs[1] = scan(currencyPair,1,'/');
  curs[2] = scan(currencyPair,2,'/');
  call sortc(of curs[*]);
  new_pair = catx('/',of curs[*]);
  put _all_;
run;

You can then load the hash in that same datastep.  Using call sortc will sort the variables alphabetically, so that you have a single currency pair.  You can then test its presence and add it if needed without having to test twice.
I would also express a general preference for storing it with two keys (the two currencies) rather than with a single merged key, but there may be reasons for not doing that as well in your application.  Two keys tends to be easier to work with in applications like this in my experience.

A: If I understand your question correctly, maybe you could try this:
data want;
   if _n_=1 then do;
      declare hash h();
      h.definekey('CurrencyPair');
      h.definedata('CurrencyPair');
      h.definedone();
   end;
   set have;
   _CurrencyPair=prxchange('s/(.*)\/(.*)/$2\/$1/',-1,strip(CurrencyPair));
   rc1=h.check();
   rc2=h.check(key:_CurrencyPair);
   if rc1^=0 and rc2^=0  then do;
      h.add();
      CurrencyPairRecode = CurrencyPair;
   end;
   else if rc1^=0 and rc2=0 then do;
      h.add();
      CurrencyPairRecode =_CurrencyPair;
   end;
   else if rc1=0 then CurrencyPairRecode = CurrencyPair;
drop rc: _:;
run;

