我有一个包含10个值的表,这些值将在宏中一一用作输入。 下面是结构和表名
Table b
Column 1 Column 2
P_0 10
P_10 34
P_20 55
P_30 67
我需要使用上述值重新运行以下代码并附加数据
proc sql;
create table a1 as
select * from table a
where amount>=p_0;
proc sql;
create table a2 as
select * from table a
where amount>=p_10;
......
如何在SAS中定义宏变量并为其编写宏
答案 0 :(得分:1)
您不需要宏-使用调用执行:
data _null_;
set b;
if _n_=1 then call execute('proc sql;');
str1=cats('create table a',_n_);
retain str2 ' as select * from table a where amount>=';
call execute(str1!!str2!!'column 1'n!!";');
run;
这将在调用堆栈中生成以下内容,并在数据步骤之后大量运行:
proc sql;
create table a1 as select * from table a where amount>= P_0;
create table a2 as select * from table a where amount>= P_10;
create table a3 as select * from table a where amount>= P_20;
create table a4 as select * from table a where amount>= P_30;
答案 1 :(得分:0)
SQL INTO
子句会将数据集变量值分配给宏变量值。
data have;
do acctid = 1 to 10;
do _n_ = 1 to 200;
txid + 1;
amount = floor(100*ranuni(123))-25;
array v(10) (1:10);
output;
end;
end;
run;
data params; input name $ value;datalines;
P_0 10
P_10 34
P_20 55
P_30 67
run;
%macro foo;
proc sql noprint;
select name, value into :name1-, :value1-
from params;
%local N i;
%let N = &SQLOBS;
data
%do i = 1 %to &N;
want&i
%end;
;
set have;
* complicated stuff involving v1-v10;
%do i = 1 %to &N;
if amount >= &&value&i then do;
splitfor = "&&name&i";
OUTPUT want&i;
end;
%end;
run;
%mend;
options mprint;
%foo;
由于宏用于生成源代码,因此在需要高精度比较时不应使用此技术。