2015-10-12 64 views
1

我是SAS新手,尝试创建一个涉及proc sql的用户定义函数,该函数的简化版本如下所示;在proc fcmp中使用proc sql命令?

proc fcmp outlib=work.funcs.test; 
function calculate(table1, var1, motherTable); 
proc sql noprint; 
    create table table1 as 
    select var1 
    from motherTable; 
quit; 
return(); 
endsub; 

然而,当我运行程序我得到如下:

ERROR: Subroutine 'calculate' was not terminated with ENDSUB. 
ERROR: File WORK.MOTHERTABLE.DATA does not exist. 

我终止与endsub()函数,我知道motherTable不存在,因为它是一个参数尚未定义的功能。有谁知道是什么问题?非常感谢!

+1

由于您所做的只是运行SAS代码,我只需将该“功能”作为宏写入即可。 – Tom

回答

3

首先,你在做什么可能会更好地在一个宏中完成。这就是你在SAS大部分时间都是这样做的。

%macro calc_func(in_table=, out_table=, var=); 

    proc sql noprint; 
    create table &out_table. as 
     select &var. 
     from &in_table. 
    ; 
    quit; 
%mend calc_func; 

所有第二,你可以一个用户定义的函数做这个(或用户定义的调用程序,更有可能,因为没有什么这里返回);但如果我的理解是正确的,你必须通过这个宏。

查看this paper了解更多信息,或者查看下面的示例。

%macro calc_func(); 
    %let table1=%sysfunc(dequote(&table1.)); 
    %let var1=%sysfunc(dequote(&var1.)); 
    %let motherTable=%sysfunc(dequote(&motherTable.)); 

    %put _all_; 

    proc sql; 
    create table &table1. as (
     select &var1. 
     from sashelp.&motherTable.) 
    ; 
    quit; 
%mend calc_func; 

proc fcmp outlib=work.funcs.test; 
    function calculate(table1 $, var1 $, motherTable $); 
    rc = run_macro('calc_func', motherTable, table1, var1); 
    return(rc); 
    endsub; 
quit; 

options cmplib=work.funcs; 
data _null_; 
    x = calculate('newclass', 'age', 'class'); 
    put x=; 
run; 

基本上,RUN_MACRO取宏名称作为参数,然后允许FCMP创建具有FCMP变量(或传递的参数)的名称宏变量。但是,你必须删除他们的报价,这是...恼人的。不要这样做的好理由,除非它真的有必要,我想。

0

PROC SQL语句正在结束PROC FCMP编译。你应该把它写成一个宏。

%macro calculate(table1, var1, motherTable); 
proc sql noprint; 
    create table &table1 as 
    select &var1 
    from &motherTable 
    ; 
quit; 
%mend calculate;