Macro returning a value

后端 未结 3 593
清酒与你
清酒与你 2021-02-13 19:56

I created the following macro. Proc power returns table pw_cout containing column Power. The data _null_ step assigns the val

3条回答
  •  庸人自扰
    2021-02-13 20:23

    @itzy is correct in pointing out why your approach won't work. But there is a solution maintaing the spirit of your approach: you need to create a power-calculation function uisng PROC FCMP. In fact, AFAIK, to call a procedure from within a function in PROC FCMP, you need to wrap the call in a macro, so you are almost there.

    Here is your macro - slightly modified (mostly to fix the symput statement):

    %macro ttest_power;
    
      proc power; 
         twosamplemeans test=diff_satt 
         groupmeans = &meanA | &meanB 
         groupstddevs = &stdA | &stdB
         groupns = (&nA &nB)
         power = .;    
         ods output Output=pw_out;
      run;
    
      data _null_;
          set pw_out;
          call symput('tpw', power);
      run;
    
    %mend ttest_power;
    

    Now we create a function that will call it:

    proc fcmp outlib=work.funcs.test;
    
      function ttest_power_fun(meanA, stdA, nA, meanB, stdB, nB);
        rc = run_macro('ttest_power', meanA, stdA, nA, meanB, stdB, nB, tpw);
        if rc = 0 then return(tpw);
        else return(.);
       endsub;
    
    run; 
    

    And finally, we can try using this function in a data step:

    options cmplib=work.funcs;
    
    data test;
       input a s1 n1 a2 s2 n2;
       pw_tmp=ttest_power_fun(a, s1, n1, a2, s2, n2);
     cards;
    0 1 10 0 1 10
    0 1 10 1 1 10
    ;
    run;
    
    proc print data=test;
    

提交回复
热议问题