Friday, October 4, 2013

Tip on Hash declaration

If you are familiar with hash programming, you must be familiar with the standard hash code template at below:
length c1 c2 $ 20;
if _n_=1 then do;
    declare hash h (dataset:"dsn");
    rc = h.definekey('n1', 'c1');
    rc = h.definedata('c2');
    rc = definedone();
    call missing(n1, c1, c2);
end;

Below is the SAS explanation: The hash object does not assign values to key variables (for example, h.find(key:'abc')), and the SAS compiler cannot detect the data variable assignments that are performed by the hash object and the hash iterator. Therefore, if no assignment to a key or data variable appears in the program, SAS issues a note stating that the variable is uninitialized.
So, we can have simpler and robust codes:
if _n_=1 then do;
    if 0 then set dsn (keep=n1 c1 c2); /* copy variable metadata from original dataset */
    declare hash h (dataset:"dsn");
    rc = h.definekey('n1', 'c1');
    rc = h.definedata('c2');
    rc = definedone();
end;

No comments: