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

1 comment:

Unknown said...

Liked your options set... article. Today I was actually doing the same at work and thought this macro might be helpful as a short cut.

Cheers,
Ben Wilde.

/* Set up environment variables - to emulate the shell invocation that would be run from our Job scheduler ControlM */
/* We will use "options set=...." statments to set environment variables that would normally be done via the shell */

%macro setvar(vname=, value=);
%if &vname ne %then
%do;
option set=&vname "&value";
%global &vname;
%let &vname=%sysget(&vname);
%end;
%mend;

%setvar(vname=env, value=dev);
%setvar(vname=ODATE, value=20180708);