简体   繁体   English

SAS-在宏中调用proc sql之后的宏

[英]SAS - A Macro following a proc sql called in a macro

New here, so if I did something wrong, I apologize. 这是新内容,因此,如果我做错了什么,我深表歉意。 I'm new user of SAS as well. 我也是SAS的新用户。

I created a macro that calls first a proc sql that creates a certain table that I want to pass it to another macro (inside the first macro). 我创建了一个宏,该宏首先调用proc sql,该proc创建一个特定的表,该表我希望将其传递给另一个宏(在第一个宏内部)。

%Macro Mc_Copy_Table (TABLE_NAME);
  proc sql;
    create table &TABLE_NAME as 
    select *
    from OR_IN.&TABLE_NAME;

    connect using OR_OUT;
    execute (truncate table &TABLE_NAME) By OR_OUT;
    disconnect from OR_OUT;
  quit;

  %MC_obsnvars(&TABLE_NAME);

  %put &Nobs;
  %if &Nobs > 100000 %then
    %do; /* use of the sql loader */
    proc append base = OR_OU. &TABLE_NAME (&BULKLOAD_OPTION)
                data = &TABLE_NAME;
    run;
    %end;
  %else
    %do;
    proc append base = OR_OU. &TABLE_NAME (Insertbuff=10000)
                data = &TABLE_NAME;
    run;
    %end;
%Mend Mc_Copy_Table;

The Mc_Obsnvars macro use the attrn function to get the number of observations from the given dataset (it opens the dataset first). Mc_Obsnvars宏使用attrn函数从给定的数据集中获取观察数(首先打开数据集)。 Depending on the number of observations, my program either use the sqlloader or not. 根据观察的次数,我的程序是否使用sqlloader。 OR_IN and OR_OUT are libnames (oracle engine). OR_IN和OR_OUT是libname(Oracle引擎)。

When The macro Mc_Copy_Table is executed, with let's say TABLE1 as argument, the Mc_Obsnvars is executed first which tries to open TABLE1 which doesn't exist yet. 当执行宏Mc_Copy_Table时,以TABLE1作为参数,首先执行Mc_Obsnvars,这将尝试打开尚不存在的TABLE1。 The proc sql is executed afterwards. proc sql之后执行。

Why the macro is executed before the proc sql ? 为什么宏在proc sql之前执行? and is there any way to have the proc sql be executed first ? 有什么办法让proc sql首先执行? putting the proc sql part in a macro doesn't solve the problem. proc sql部分放在宏中并不能解决问题。 Thanks :) 谢谢 :)

I think you have a syntax issue, as Quentin alludes to in his comment. 我认为您有语法问题,正如昆汀在他的评论中提到的那样。 This works OK for me: 这对我来说行得通:

%macro copy_table(intable, outtable);
proc sql noprint;
create table &outtable as
select * from &intable;

%count_obs(&outtable);
%put NOBS:&nobs;
quit;
%mend;

%macro count_obs(table);
%global nobs;
select count(*) into :nobs trimmed from &table;
%mend;

data test;
do i=1 to 10;
    output;
end;
run;

%copy_table(test,test2);

Note however, you don't have to do the count. 但是请注意,您不必进行计数。 There is an automatic variable from PROC SQL called &sqlobs with the number of records returned from the last query. PROC SQL中有一个自动变量,称为&sqlobs具有上一次查询返回的记录数。

So this gives you what you are looking for, I think: 因此,我认为这为您提供了所需的东西:

%macro copy_table(intable, outtable);
proc sql noprint;
create table &outtable as
select * from &intable
where i < 5;

%let nobs=&sqlobs;
%put NOBS:&nobs;
quit;
%mend;
%copy_table(test,test2);

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

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