是否可以读取(并执行)存储在 SAS 数据集中的字符串中的 SAS 代码。
例如,数据集“CODE”包含一个字符串变量,其中包含
"IF TOTAL_SALES GE 20000 AND TYPE IN ('A', 'B', 'C') THEN VAR1 = 'Y' ;"
我可以做类似的事情吗?
data sales ;
set sales ;
/* run the if statement above */
run ;
使用 SAS 9.2
有很多方法可以做到这一点,因为有很多方法可以构造宏变量。通常最有用的三个:
proc sql 选择进入
这使您可以立即创建多行代码,并且可能是用于此目的的最常用工具。它可以直接创建代码行,或者更有用地创建宏调用。例如,假设您想运行:
data want;
set have;
x = sum(a,b,c);
run;
但是 x,a,b,c 都在另一个数据集中定义。此外,您有 3 个这样的变量。
data callset;
input var1 $ var2 $ var3 $ var4 $;
datalines;
x a b c
y d e f
z b e c
;;;;
run;
你可以这样构造它:
proc sql;
select cats(x,"=sum(",a,",",b,",",c,");") into :calllist
separated by ' '
from callset;
quit;
data want;
set have;
&calllist.
run;
但是,构建宏可能更容易:
%macro sum(var1,var2,var3,var4);
&var1. = sum(&var2.,&var3.,&var4.);
%mend sum;
然后您的 PROC SQL 会更容易一些(在这种情况下并非如此,但这通常有助于更复杂代码的可读性):
proc sql;
select cats('%sum(',catx(',',x,a,b,c),')') into :calllist
separated by ' '
from callset;
quit;
然后以同样的方式使用它。
这里的限制:除了 PROC SQL 允许你做的事情之外,你不能在构造字符串时修改它(这是强大的,但不是 datastep 代码,如果你需要使用像 first.var 这样的东西,你必须在proc sql 在一个单独的步骤中)。您在宏变量中总共有大约 20k 个字符的限制。
顺便说一句,这separated by
很重要;没有它,您只能创建一行代码(只有最后一行将被放入宏变量中)。即使你真的不希望它被任何东西分隔,你仍然需要用''分隔才能生成列表。
%包含文件
包含文件方法是 proc sql 方法和调用 execute 的混合体。它是在数据步骤中构建的,并且没有超出操作系统文件大小限制的长度限制。然而,它有点混乱(因为创建了一个临时文件)并且具有包含文件的正常限制,例如不包含数据行。
您以这种方式构建它(使用以前的数据集):
filename toincl temp; *create temporary fileref;
data _null_;
set callset;
file toincl;
callstr = cats('%sum(',catx(',',x,a,b,c),')');
put callstr $;
run;
data want;
set have;
%include toincl;
run;
它绕过了 PROC SQL 长度限制,但具有包含文件的正常限制(有关更多信息,请参阅文档)。
调用执行
这用于在数据步骤之后立即以交互方式执行一行代码。它很方便,因为它允许您比其他方法更灵活地动态构建代码,但它有很大的时间限制。
data _null_;
set callset; *this is not the main data set, but the control file with SAS code;
call execute('data want; set have;');
callstr=cats('%sum(',catx(',',x,a,b,c),')');
call execute(callstr);
call execute('run;');
run;
人们通常遇到的主要限制是宏观可变时间。在 CALL EXECUTE 步骤中定义宏变量时,在同一 CALL EXECUTE 步骤中不能使用它。因此,如果您的代码包含要创建然后使用宏变量的代码,它将无法正常运行;您需要使用其他方法之一。如果您使用这种方法,我强烈建议您先阅读几篇关于 CALL EXECUTE 的论文,例如这篇或这篇。
2种方法来做到这一点:
对于调用执行,请参见:http: //support.sas.com/documentation/cdl/en/mcrollref/61885/HTML/default/viewer.htm#a000543697.htm
sales
在读入数据之前,您希望在创建包含 SAS 代码的宏变量之前使用 call symput 函数。这是一些示例代码:
* _null_ means there is no output dataset;
data _null_;
set code;
* assuming char variable containing the SAS code is called 'code_string';
* if only certain rows contain the correct code_string values;
* then you could use an if statement to assign &my_code as desired;
call symput('my_code', code_string);
run;
data sales;
set sales;
* now access the value of the macro variable created with call symputx above;
&my_code
run;
如果code
具有包含 SAS 代码字符串的 char 变量的数据集只有一行(或者可以通过where
语句限制为单行),那么您可以使用上述数据步骤proc sql
来实现相同的结果:_null_
proc sql;
select code_string into :my_code
from code
/* where [put your boolean expression here] */
;
quit;