在 SQL 语句中执行宏
Execute Macro inside SQL statement
情况:
我有一个包含两列的 table mytable
:tablename
和 tablefield
:
|-----------|------------|
| tablename | tablefield |
|-----------|------------|
| table1 | id |
| table2 | date |
| table3 | etc |
|-----------|------------|
我的核心objective这里基本上是,为每个制作一个Select
[=49] =]名称,显示其相应 table 字段的 MAX()
值。
Proc SQL;
Select MAX(id) From table1;
Select MAX(date) From table2;
Select MAX(etc) From table3;
Quit;
ps:解决方案必须从table中提取数据,所以无论table改变它的值,解决方案也会改变它.
我尝试过的:
从我的大部分尝试来看,这是最复杂的,我认为最接近解决方案:
proc sql;
create table table_associations (
memname varchar(255), dt_name varchar(255)
);
Insert Into table_associations
values ("table1", "id")
values ("table2", "date")
values ("table3", "etc");
quit;
%Macro Max(field, table);
Select MAX(&field.) From &table.;
%mend;
proc sql;
Select table, field, (%Max(field,table))
From LIB.table_associations
quit;
创建宏,我的意图很明确,但是对于这个例子,我应该解决 2 个问题:
- 在
SQL
语句中执行宏;并且
- 使宏将其字符串值参数理解为
SQL
命令。
在数据步骤中,您可以使用 call execute
执行您所描述的操作。
%Macro Max(field, table);
proc sql;
Select MAX(&field.) From &table.;
quit;
%mend;
data _null_;
set table_associations;
call execute('%MAX('||field||','||table||')');
run;
此处不需要宏,因为您可以在数据步骤中使用 put
语句生成代码:
filename gencode temp;
data _null_;
set table_associations end=eof;
file gencode;
if _n_=1 then put 'proc sql;';
put 'select max(' tablefield ') from ' tablename ';';
if eof then put 'quit;';
run;
%include gencode / source2;
filename gencode clear;
代码被写入名为 'gencode' 的临时文件。如果需要,您可以永久保存此文件。 _n_=1
和 end=eof
用于打印查询前后的语句。最后,%include gencode
运行代码,source2
选项将代码打印到日志中。
情况:
我有一个包含两列的 table mytable
:tablename
和 tablefield
:
|-----------|------------|
| tablename | tablefield |
|-----------|------------|
| table1 | id |
| table2 | date |
| table3 | etc |
|-----------|------------|
我的核心objective这里基本上是,为每个制作一个Select
[=49] =]名称,显示其相应 table 字段的 MAX()
值。
Proc SQL;
Select MAX(id) From table1;
Select MAX(date) From table2;
Select MAX(etc) From table3;
Quit;
ps:解决方案必须从table中提取数据,所以无论table改变它的值,解决方案也会改变它.
我尝试过的:
从我的大部分尝试来看,这是最复杂的,我认为最接近解决方案:
proc sql;
create table table_associations (
memname varchar(255), dt_name varchar(255)
);
Insert Into table_associations
values ("table1", "id")
values ("table2", "date")
values ("table3", "etc");
quit;
%Macro Max(field, table);
Select MAX(&field.) From &table.;
%mend;
proc sql;
Select table, field, (%Max(field,table))
From LIB.table_associations
quit;
创建宏,我的意图很明确,但是对于这个例子,我应该解决 2 个问题:
- 在
SQL
语句中执行宏;并且 - 使宏将其字符串值参数理解为
SQL
命令。
在数据步骤中,您可以使用 call execute
执行您所描述的操作。
%Macro Max(field, table);
proc sql;
Select MAX(&field.) From &table.;
quit;
%mend;
data _null_;
set table_associations;
call execute('%MAX('||field||','||table||')');
run;
此处不需要宏,因为您可以在数据步骤中使用 put
语句生成代码:
filename gencode temp;
data _null_;
set table_associations end=eof;
file gencode;
if _n_=1 then put 'proc sql;';
put 'select max(' tablefield ') from ' tablename ';';
if eof then put 'quit;';
run;
%include gencode / source2;
filename gencode clear;
代码被写入名为 'gencode' 的临时文件。如果需要,您可以永久保存此文件。 _n_=1
和 end=eof
用于打印查询前后的语句。最后,%include gencode
运行代码,source2
选项将代码打印到日志中。