Lovnlust
Lovnlust

Reputation: 1537

use format name as function input in proc fcmp

I have two different formats defined.

proc format;
value fmtA
1 = 3
2 = 5
;
value fmtB
1 = 2
2 = 4
;
run;

function myfun returns formatted value

proc fcmp outlib=WORK.pac.funcs;
    function myfun(n);
        val = put(n,fmta.);
        return (val);
    endsub;
run;

I want to make this a bit more dynamic - val will be based on function input.

EDIT

proc fcmp outlib=WORK.pac.funcs;
    function myfun(n,myfmt $);
        if myfmt = 'fmtA' then val = put(n,fmtA.);
        else if myfmt = 'fmtB' then val = put(n,fmtB.);
        else val = n;
        return (val);
    endsub;
run;

data test;
    n = 2;
    myfmt = 'fmtA';
    output;
    myfmt = 'fmtB';
    output;
    myfmt = 'fmtC';
    output;
run;

data test2;
  set test;
    /* try to do sth like this */
    value = myfun(n,myfmt);
run;

This solution works. However it requires a long list of checking when I have so many different formats. And it's not possible before I take a look at format name in the input test dataset.

Upvotes: 1

Views: 206

Answers (1)

Tom
Tom

Reputation: 51566

Just use the PUTN() function.

proc format;
value fmtA 1 = '3' 2 = '5' ;
value fmtB 1 = '2' 2 = '4' ;
value fmtc 1 = '1' 2 = '3' ;
run;
data test;
  do myfmt = 'fmtA.','fmtB.','fmtC.';
  do n= 1,2;
    str = putn(n,myfmt);
    value = input(str,32.);
    output;
  end;
  end;
run;

Upvotes: 3

Related Questions