你如何在SAS中使用用户定义的函数

Met*_*est 0 sas

我是 SAS 的新手。我试图在 SAS 中使用自定义函数,但它不起作用。我尝试了以下方法。

....
proc fcmp outlib=work.f.f;
    function FtoC(temp);              
        Celsius = (5/9*(temp-32));     
        return(Celsius);               
    end func; /* This is line 22 */
run;
quit;

options cmplib=work.f;
DATA WORK.IMPORT;
    set WORK.IMPORT;
    Celsius = FtoC(temp);   
run;
....
Run Code Online (Sandbox Code Playgroud)

但得到以下错误。我在做什么错误?

ERROR 22-322: Expecting ;.  
ERROR 202-322: The option or parameter is not recognized and will be ignored.
ERROR 68-185: The function FTOC is unknown, or cannot be accessed.
ERROR: Variable CELSIUS not found.
ERROR: Variable Celsius is not on file WORK.IMPORT.
Run Code Online (Sandbox Code Playgroud)

Tom*_*Tom 5

请注意,完整的 SAS 日志将为错误消息添加更多上下文。

1     proc fcmp outlib=work.f.f;
2         function FtoC(temp);
3             Celsius = (5/9*(temp-32));
4             return(Celsius);
5         end func; /* This is line 22 */
              ----
              22
              202
ERROR 22-322: Expecting ;.
ERROR 202-322: The option or parameter is not recognized and will be ignored.
6     run;

NOTE: Execution aborted because of errors in program.
      Reenter the corrected program or enter "QUIT;" to exit procedure.
7     quit;
Run Code Online (Sandbox Code Playgroud)

通过使用 ENDSUB 结束函数定义来修复第一个错误。

15    proc fcmp outlib=work.f.f;
16        function FtoC(temp);
17            Celsius = (5/9*(temp-32));
18            return(Celsius);
19        endsub;
20    run;

NOTE: Function FtoC saved to work.f.f.
NOTE: PROCEDURE FCMP used (Total process time):
      real time           0.07 seconds
      cpu time            0.06 seconds
Run Code Online (Sandbox Code Playgroud)

这将使第二个错误消失。