简体   繁体   English

SAS Datetime Proc SQL

[英]SAS Datetime Proc SQL

Hello I was wondering how would you write this code in a PROC SQL vs the data step I wrote below. 您好,我想知道您将如何在PROC SQL编写此代码,而不是我在下面编写的data step I am trying to reduce the code, the data is initially in a text file unfortunately the datetime when changed to a CHAR (import wizard) is a length of 9 vs 8(computed column) which is the default, hence why i change it in the first data step. 我正在尝试减少代码,不幸的是,数据最初是在文本文件中的,更改为CHAR (导入向导)时的日期时间是9 vs 8(计算列)的长度,这是默认值,因此为什么我要在其中进行更改第一步数据。 I eventually get the results I want but I would like to see if SQL could provide a more efficient solution. 我最终得到了想要的结果,但我想看看SQL是否可以提供更有效的解决方案。

data WORK.CNE_RESI;
  SET WORK.cneres_41;
  FORMAT RPTDATE_2 $CHAR9.;
  IF rptdate = '1/5/2015' THEN  RPTDATE_2 = '1/9/2015'; 
  ELSE IF RPTDATE_2 = "" THEN  RPTDATE_2=rptdate ;
RUN;

data WORK.CNE_RESI_2;
  SET WORK.CNE_RESI;
  FORMAT RPTDATE_3 MMDDYY10.;
  RPTDATE = input(RPTDATE_2, MMDDYY10.);
RUN;

Not sure if this is the right way to do it but I had a go. 不知道这是否是正确的方法,但是我已经走了。

%let olddate = 1/5/2015;
%let newdate = 1/9/2015;


proc sql;

create table WORK.CNE_RESI_2 as
select a.*,
case when rptdate = "&olddate" then "&newdate"
else rptdate
end as RPTDATE_2 format=$char9.,
input(case when rptdate = "&olddate" then "&newdate"
else rptdate
end,mmddyy10.) as RPTDATE_3 format=mmddyy10.
from WORK.cneres_41 a;
quit;

Of course if you didn't actually need the variable rptdate_2 and were just using that to change format then this should work. 当然,如果您实际上并不需要变量rptdate_2,而只是使用它来更改格式,那么这应该可以工作。

proc sql;
    create table WORK.CNE_RESI_2 as
    select a.*,
    input(case when rptdate = "&olddate" then "&newdate"
          else rptdate
          end,mmddyy10.) as RPTDATE_3 format=mmddyy10.
from WORK.cneres_41 a;
quit;

Your ultimate question is: 您的最终问题是:

I eventually get the results I want but I would like to see if SQL could provide a more efficient solution. 我最终得到了想要的结果,但我想看看SQL是否可以提供更有效的解决方案。

The reason that your DATA steps seem inefficient is that you make two complete passes over the data. DATA步骤似乎效率低下的原因是您对数据进行了两次完整传递。 There's no reason for that in this case, and a single DATA step is likely to be at least as efficient as SQL for your example. 在这种情况下,没有理由这样做,对于您的示例,单个DATA步骤可能至少与SQL一样有效。 Also, placing the format statement above the set statement will redefine the length of rptdate without the need for an intermediate variable. 另外,将format语句放在set语句上方将重新定义rptdate的长度,而无需中间变量。 With these thoughts in mind, your two DATA steps could be more efficiently written as: 考虑到这些想法,您的两个DATA步骤可以更有效地编写为:

data WORK.CNE_RESI;
    format rptdate $char10. rptdate_n mmddyy10.; 
    set WORK.cneres_41;

    if rptdate = '1/05/2015' then rptdate = '1/09/2015';
    rptdate_n = input(rptdate, ?? MMDDYY10.);
run;

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

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