繁体   English   中英

如何在表中分配宏变量值并在SAS中附加宏输出的数据

[英]How can i assign macro variable value from a table and append data of a macro output in SAS

我有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中定义宏变量并为其编写宏

您不需要宏-使用调用执行:

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;

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;

由于宏用于源代码生成,因此在需要高精度比较时不应使用此技术。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM