sas - SAS:使用调用符号宏变量作为过滤标准
问题描述
我有一个具有字段 Income 、 Age 和 Cond 的数据,其中 A 和 B 是数字,而 Cond 包含条件(字符串),例如“If Income>=10000”、“If Age<=35”等。我想使用字段 Cond 进行过滤的数据。我正在使用 call symput 在数据步骤中创建运行时宏变量 bt 无法将其用作过滤标准。
data T2;
set T1;
CALL SYMPUT("Condition", Cond);
&Condition.; /*this is the line which is not working*/
run;
解决方案
您正在混合范围。
一个正在运行的数据步骤不能改变它正在运行的源代码,所以你不能让一个数据步骤设置一个宏变量值,然后期望数据集使用宏变量的分辨率作为源代码。
您可以使用多种技术来评估表达式。
调用执行
您可以在 DATA 步运行时在宏环境中使用call EXECUTE
表达式%EVAL
。结果可以用SYMGET
想法的例子
%let x = 0;
data _null_;
length expression $1000;
expression = '%let x = %eval(10 + 20)';
call execute (expression);
x = symget('x');
put x=;
run;
使用这个想法
data want;
set have;
condition = tranwrd(condition, 'age', cats(age));
condition = tranwrd(condition, 'income', cats(income));
call execute (cats('%let result = %eval(', condition, ')'));
result = symget('result');
* subsetting if based on dynamic evaluation of conditional expression;
if result;
run;
其他
执行动态代码的其他方法是通过函数RESOLVE或DOSUBL
推荐阅读
- computer-vision - 单应矩阵分解——从右手坐标系到左手坐标系的转换
- r - 在没有 png dev.off() 的情况下在 R 中保存绘图
- kotlin - 改造为所有端点插入相同的查询参数
- laravel - Laravel如何自动删除表然后导入sql文件然后应用迁移?
- scala - 如何发布带有校验和(MD5、SHA1)的工件?
- python - 单击按钮时打开窗口,关闭新窗口时隐藏父级并再次显示父级pyqt
- javascript - 在我的 redux 商店中,我的套接字放在哪里?
- c# - 在 MS Access 中过滤存储查询的结果
- node.js - 使用 .js 函数获取 .ejs 的价值
- java - 为什么 Jacoco 合并不包括文件?