SAS: proc fcmp.sas

***********数值型**********;
proc fcmp outlib=work.scrambling.test;
    function change(lb);
        kg=lb/2.2;
        return(kg);
    endsub;
quit;

options cmplib=(work.scrambling);

data a;
    set sashelp.class(keep=name age weight);
    kilos=change(weight);
run;

*********字符型********;

proc fcmp outlib=work.scrambling.test;
    function lb2kgc(lb) $;
        length kg $10;
        kg = catt(put((lb/2.2),6.2),'kg');
        return (kg);
    endsub;
run;

options cmplib=(work.scrambling);

data b;
    set sashelp.class(keep=name age weight);
    kilos=change(weight);
    kg_c=lb2kgc(weight);
run;

********两个自变量*********;

proc fcmp outlib=work.scrambling.test;
    function bmi(lb,ht);
        return((lb*703)/(ht*ht));
    endsub;
quit;

options cmplib=(work.scrambling);

data bmi;
    set sashelp.class(keep=name age weight height);
    bmi=bmi(weight,height);
run;

********自变量和return值放在一起*********;

proc fcmp outlib=work.scrambling.test;
    subroutine biomassindex(w,h,b);
        outargs b;
        b= ((w*703)/(h*h));
    endsub;
quit;

options cmplib=(work.scrambling);

data bmi;
    set sashelp.class(keep=name age weight height);
    bmindex=.;
    call biomassindex(weight,height,bmindex);
run;

*********函数里面是可以用逻辑语句的********;

proc fcmp outlib=work.scrambling.test;
    function fromto(code $,v);
        if upcase(code)='LB2KG' then r=V/2.2;
        else if upcase(code)='KG2LB' then r=v*2.2;
        else r=.;
        return (r);
    endsub;
quit;

option cmplib=(work.scrambling);

data conv;
    set sashelp.class(keep=name age weight);
    kilos=fromto('lb2kg',weight);
    pounds=fromto('kg2lb',kilos);
run;

*****************;

%macro printit(lib,dsn,num);
    %put &lib &dsn;
    %let lib = %sysfunc(dequote(&lib));
    %let dsn = %sysfunc(dequote(&dsn));
    %let num = %sysfunc(dequote(&num));
    %if &num = %then %let num=max;
    title2 "&lib..&dsn";
    title3 "first &num observations";
    proc print data=&lib..&dsn(obs=&num);
    quit;
%mend printit;

proc fcmp outlib=work.scrambling.test;
    subroutine printN(lib,dsn,num);
        rc=run_macro('printit',lib,dsn,num);
    endsub;
quit;

******************;
proc fcmp outlib=work.scrambling.test;
    subroutine metric_hwbmi(h,w,mh,mw,bmi);
        outargs mh,mw,bmi;
        mh = h*.0254;
        mw = w*.4536;
        bmi = mw/(mh*mh);
    endsub;
quit;

options cmplib = (work.scrambling);

data multiple;
    set sashelp.class(keep=name age height weight);
    heightmeters=.;
    weightkilos=.;
    bmi=.;
    call metric_hwbmi(height,weight,heightmeters,weightkilos,bmi);
run;

***********************;
options cmplib=(work.scrambling);
%let ht = 69;
%let wt = 112.5;
%let bmi = %sysfunc(bmi(&wt,&ht));
%put &bmi;

*********删除函数*******;
proc fcmp outlib=work.scrambling.test;
    deletefunc lb2kgc;
    deletefunc biomassindex;
run;

***************************;
proc fcmp outlib=sasuser.funcs.math;

    subroutine subA();
        x = 5;
        call subB();
        put 'In subA:' x=;
    endsub;

    subroutine subB();
        x = 'subB';
        put 'In subB:' x=;
    endsub;

run;
options cmplib=sasuser.funcs;
data _null_;
    x = 99;
    call subA();
    put 'In DATA step: ' x=;
run;

***************************;
proc fcmp outlib=sasuser.funcs.math;
    subroutine allpermk(n, k);
        array scratch[1]/nosymbols;
        call dynamic_array(scratch, n);
        call permk(n,k,scratch,1,0);
    endsub;
    subroutine permk(n,k,scratch[*],m,i);
        outargs scratch;
        if m-1 = n then do;
            if i = k then put scratch[*];
        end;
        else do;
            scratch[m] = 1;
            call permk(n, k, scratch, m+1, i+1);
            scratch[m] = 0;
            call permk(n, k, scratch, m+1, i);
        end;
    endsub;
quit;

options cmplib=sasuser.funcs;

data _null_;
    call allpermk(5,3);
run;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。