簡體   English   中英

SAS數據步驟與proc SQL日期

[英]SAS data step vs. proc sql dates

我希望有人可以幫助我回答以下查詢:我有兩個程序,一個在proc sql中,一個在數據步驟中。 proc sql有效,數據步驟無效。 我不明白為什么?

%let _run_date = '30-jun-2017';
proc sql;
        connect to oracle (path='EDRPRD' authdomain='EDRProduction' 
buffsize=32767);
        create table customer_sets as                              
        select * from connection to oracle (
        select *
        from    customer_set
        where   start_date <= &_run_date.
        and     nvl(end_date, &_run_date.) >= &_run_date.
        and     substr(sets_num,1,2) = 'R9');
quit; 

這很好。 但是,這不是:

libname ora oracle path='EDRPRD' authdomain='EDRProduction' schema='CST'; 
data customer_sets;
    set ora.customer_set;
    where   start_date le &_run_date. and
            coalesce(end_date, &_run_date.) ge &_run_date. and
            substr(sets_num,1,2) = "R9";
run;

誰能告訴我為什么?

謝謝!

可能會有助於查看錯誤日志,但是,對於初學者來說,日期宏變量(在數據步驟中使用)會被SAS解釋為字符串文字,而不是日期。 在SAS中,日期文字用引號引起來(單引號或雙引號),后跟d 您可以按照以下步驟修改數據步驟,看看效果是否更好:

%let _run_date = '30-jun-2017';
data customer_sets;
    set ora.customer_set;
    where   start_date le &_run_date.d and
            coalesce(end_date, &_run_date.d) ge &_run_date.d and
            substr(sets_num,1,2) = "R9";
run;

如果這不是問題,請發布包含錯誤的日志。

編輯

這是上面的代碼,帶有預先創建的少量測試數據:

libname ora (work);
data ora.customer_set;
infile datalines dlm='09'x;
input ID start_date :anydtdte. end_date :anydtdte. sets_num $;
format start_date end_date date.;
datalines;
1   30-may-2017 .   R9xxx
2   30-may-2017 31-may-2017 R9xxx
;
run;

%let _run_date = '30-jun-2017';
data customer_sets;
    set ora.customer_set;
    where   start_date le &_run_date.d and
            coalesce(end_date, &_run_date.d) ge &_run_date.d and
            substr(sets_num,1,2) = "R9";
run;

您可以復制粘貼並按原樣運行,您會發現它工作正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM