Wednesday, October 18, 2017

Lookup using PROC FCMP data type dictionary

With 9.4M5, we will have one new technique - PROC FCMP dictionary. I wouldn't believe the new data type will be added in DATA STEP/DS2. We have to leverage the new data type via FCMP.
Below is sample code on how to lookup using the new technique.
proc fcmp outlib=work.funcs.test;
    subroutine s(a, b $, c $);
        outargs a, b;

        declare dictionary d;
        if c = 'add' then do;
            d[a] = b;
            d[b] = a;
        end;
        else if c = 'get_a' then do;
            a = d[b];
        end;
        else if c = 'get_b' then do;
            b = d[a];
        end;
    endsub;
run;

options cmplib=work.funcs;
data _null_;
    a = 1; b='xxx';
    call s(a, b, 'add');
    
    call missing(a);
    put "Note: get_a before " a= b=;
    call s(a, b, 'get_a');
    put "Note: get_a after " a= b=;

    call missing(b);
    put "Note: get_b before " a= b=;
    call s(a, b, 'get_b');
    put "Note: get_b after " a= b=;
run;

Output:
Note: get_a before a=. b=xxx
Note: get_a after a=1 b=xxx
Note: get_b before a=1 b=
Note: get_b after a=1 b=xxx

Tuesday, October 3, 2017

Easy way to check the file existence

SAS doc suggests "You can use SYSFILRC to confirm that a file or location is allocated before attempting to access an external file". I have not tested if it will work with special file but it works well with regular ones.

16   filename a "c:\";
17   %put &=sysfilrc;  /* file exists. */
SYSFILRC=0
18
19   filename b "c:\_not_exist";
20   %put &=sysfilrc; /* file does not exist. */
SYSFILRC=1