在 proc fcmp 中使用 proc sql 命令?
Using proc sql commands in proc fcmp?
我是 SAS 的新手,正在尝试创建一个涉及 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 不存在,因为它是尚未定义的函数的参数。有谁知道是什么问题?非常感谢!
首先,您正在做的事情最好在宏中完成。这就是您在 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 变量(或传递的参数)的名称创建宏变量。但是,您必须删除他们的引号,这很烦人。我想有充分的理由不这样做,除非确实有必要。
PROC SQL 语句正在结束 PROC FCMP 编译。你应该把它写成一个宏。
%macro calculate(table1, var1, motherTable);
proc sql noprint;
create table &table1 as
select &var1
from &motherTable
;
quit;
%mend calculate;
我是 SAS 的新手,正在尝试创建一个涉及 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 不存在,因为它是尚未定义的函数的参数。有谁知道是什么问题?非常感谢!
首先,您正在做的事情最好在宏中完成。这就是您在 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 变量(或传递的参数)的名称创建宏变量。但是,您必须删除他们的引号,这很烦人。我想有充分的理由不这样做,除非确实有必要。
PROC SQL 语句正在结束 PROC FCMP 编译。你应该把它写成一个宏。
%macro calculate(table1, var1, motherTable);
proc sql noprint;
create table &table1 as
select &var1
from &motherTable
;
quit;
%mend calculate;