SAS:将宏参数中的日期作为过滤器传递给oracle

u2nhd7ah  于 2023-08-04  发布在  Oracle
关注(0)|答案(1)|浏览(104)

我正在尝试创建一个宏程序,该程序迭代Oracle中的数据集并过滤publish_yr_mth变量和发布日期。所以我会有一个类似201001(Jan 2010)的发布日期,然后publish_dt>01-JAN-2010。然后移动到下一组日期(但不总是以偶数间隔,只是数据集中设置的任何内容)。
当我创建宏时,它将与publish_yr_mnth一起工作,但我甚至不能为publish_dt输入一个字符串,否则宏将无法运行。如何在此宏中向Oracle传递可读的日期。在宏之外,“DD-MON-YYYY”被接受为正确的格式,并将返回结果。
还是有其他原因导致它无法运行?
将运行的非宏版本:

proc sql ;

connect to oracle as mydb(user=user orapw="&dbpass" path=oracledb );

create table bk_samp as select * from connection to mdb (

     select author ,title from books_db where publish_dt >=’01-JAN-2010’ and publish_yr_mnth=201001  ) ;

quit;

字符串
我要运行的宏版本:

data months;
input publishdt publish_yr_mnth;
datalines ;
201001 01-JAN-2010
201002 02-FEB-2010
201005 02-MAY-2010
;

proc sql ;
select distinct publishdt
into: publishdt1-
from months;
quit

%macro books(publishdt, publish_yr_mnth): 
%do i =1 %to &sqlobs; proc sql ; 
connect to oracle as mydb(user=user orapw="&dbpass" path=oracledb );
 create table bk_samp as select * from connection to mdb (
      select author ,title from books_db where publish_dt >=&&publishdt&i and publish_yr_mnth=&&publishyrmth&i
 ) ;
 quit; 
%end; 
%mend books
%books();


我尝试过将oracle日期作为字符串和不同的日期字符传递,甚至只是在宏中没有参数的情况下硬编码,但如果我在宏中有publish_dt条件,则什么都不会运行。

mwecs4sa

mwecs4sa1#

使宏只接受一组值作为输入。如果ORACLE需要像'dd-MON-yyy'这样的字符串作为字段,那么将其传递到宏中。

%macro books(publishdt, publish_yr_mnth, outds): 
proc sql;
connect to oracle as mydb(user=user orapw="&dbpass" path=oracledb );
create table &outds as select * 
  from connection to mdb (
    select author ,title
    from books_db
    where publish_dt >=&publishdt
      and publish_yr_mnth= &publishyrmth
   ) 
;
quit; 
%mend books;

字符串
然后为驱动程序表中的每个观察调用一次。

data months;
  input publishdt publish_yr_mnth :date.;
  format publish_yr_mnth date9.;
datalines ;
201001 01-JAN-2010
201002 02-FEB-2010
201005 02-MAY-2010
;

data _null_;
  set months;
  call execute(cats('%nrstr(%books)(
              ,'publishdt=',publishdt 
              ,',publish_yr_mnth=',quote(put(publish_yr_mnth,date11.),"'")
              ,',outds=temp',_n_
              ,')'
  ));
run;

相关问题