Thursday, September 11, 2014

Table lookup using FCMP hashing

There are many way to look up table: SQL join, Data hashing, Format, and etc.
Starting from SAS 9.3, we can use hashing technique in PROC FCMP. In this way, we can lookup table using function. Below is the sample code:
* Scenario: Replace PROD_HIER_SK with PROD_ID.;
proc fcmp outlib=work.funcs.prod;
 function prod_id(in_prod_hier_sk) $ 200;
  length prod_id $ 200;

  /* executable for only once */
  declare hash h(dataset:"di_dm.product_dm");
  rc = h.definekey("prod_hier_sk");
  rc = h.definedata("prod_id");
  rc = h.definedone();

  /* look up first and return if found */
  call missing(prod_id);
  prod_hier_sk = in_prod_hier_sk;
  rc = h.find();

  return(prod_id);
 endsub;
run;

options cmplib=(work.funcs);

data sample_fcmp_hashing (keep=prod_hier_sk prod_id parent_prod_hier_sk parent_prod_id);
 set di_dm.product_hier_assoc_dm;

 prod_id = prod_id(prod_hier_sk);
 parent_prod_id = prod_id(parent_prod_hier_sk);
run;


Reference:
Andrew Henrick, Donald Erdman, and Stacey Christian. 2013. “Hashing in PROC FCMP to Enhance Your Productivity”, Proceedings of the SAS Global Forum 2013 Conference
Sample 47224: Load a SAS data set into a Hash Object using PROC FCMP

BTW, interestingly, you can find more information from SAS paper than from SAS Help doc.
As an experienced SAS programmer, I always keep an eye on SAS Global Forum.

No comments: