代码之家  ›  专栏  ›  技术社区  ›  Marco

使用直通SQL插入DB2 fom SAS数据集

  •  0
  • Marco  · 技术社区  · 9 年前

    我对SAS和DB2还是新手。我有一个DB2表,其中有一列存储编码为时间戳的值。我正试图从我的工作目录中的SAS数据集将数据加载到此列。然而,其中一些时间戳对应于01-01-1582之前的日期,不能作为日期时间值存储在SAS中。相反,它们存储为字符串。

    PROC SQL;
        connect to db2 (user = xxxx database = xxxx password = xxxx);
        execute (insert into xxxx.xxxx (var) values (TIMESTAMP('0001-01-01-00.00.00.000000'))) by db2;
        disconnect from db2;
    quit;
    

    如何为源数据集中的所有值实现这一点?A选择。。。execute命令中的from语句不起作用,因为据我所知,无法从DB2连接中引用SAS工作目录。

    提前谢谢。

    1 回复  |  直到 9 年前
        1
  •  1
  •   user2877959    9 年前

    一种复杂的解决方法是使用 call execute :

    data _null_;
    set sas_table;
    call execute("PROC SQL;
                  connect to db2 (user = xxxx database = xxxx password = xxxx);
                  execute (
                     insert into xxxx.xxxx (var)
                     values (TIMESTAMP('"||strip(dt_string)||"'))
                    ) by db2;
                  disconnect from db2;
                  quit;");
    run;
    

    哪里 sas_table dt_string .

    这里发生的是,对于数据集中的每个观察,SAS将执行 execute 调用例程,每次的当前值为

    另一种方法使用宏而不是调用execute来执行基本相同的操作:

    %macro insert_timestamp;
      %let refid = %sysfunc(open(sas_table));
      %let refrc = %sysfunc(fetch(&refid.));
      %do %while(not &refrc.);
        %let var = %sysfunc(getvarc(&refid.,%sysfunc(varnum(&refid.,dt_string))));
    
        PROC SQL;
          connect to db2 (user = xxxx database = xxxx password = xxxx);
          execute (insert into xxxx.xxxx (var) values (TIMESTAMP(%str(%')&var.%str(%')))) by db2;
         disconnect from db2;
        quit;
    
        %let refrc = %sysfunc(fetch(&refid.));
      %end;
      %let refid = %sysfunc(close(&refid.));
    %mend;
    %insert_timestamp;
    

    编辑

    libname lib db2 database=xxxx schema=xxxx user=xxxx password=xxxx;
    data lib.temp;
    set sas_table;
    run;
    PROC SQL;
        connect to db2 (user = xxxx database = xxxx password = xxxx);
        execute (create table xxxx.xxxx (var TIMESTAMP)) by db2;
        execute (insert into xxxx.xxxx select TIMESTAMP(dt_string) from xxxx.temp) by db2;
        execute (drop table xxxx.temp) by db2;
        disconnect from db2;
    quit;
    
    推荐文章