如何使其正常运行?它在复活节周末给了我错误的一天

时间:2015-02-24 21:52:07

标签: sas

所以,我想在某一天之前找到第五个工作日,这意味着不包括周末和银行假日。在正常的一周它似乎工作正常,但如果我尝试07APR2015的开始日期,这是复活节周五,周末和复活节星期一之后的星期二,它将返回错误的第五天。

txt文件包含:

bhol
01jan2015
03apr2015
06apr2015
04may2015
25may2015
31aug2015
25dec2015
28dec2015

代码如下:

options mprint mlogic symbolgen;

proc import out=bhols datafile="C:\Users\me\Documents\bhols.txt" dbms=csv replace;
label;
run;

data bholdates;
  set bhols;
  sasdate=input(bhol,date9.);
  today=input("07apr2015",date9.);
  weekday=weekday(today);
  call symputx("todayd",today,"g");
run;

proc sql ;
  select sasdate into :bhols separated by " "
  from bholdates;
quit;

%macro five;
data test;
/*  format today day day5 date9.;*/
  retain count;
  count=0;
  today=&todayd;
  day=&todayd-1;
  do until(count=1);
  weekday=weekday(day);
  if day in(&bhols) or weekday in(1,7) then do;
  day=day-1;
  end;
  if day not in(&bhols) and weekday not in(1,7) then do;
    count=count+1;
    weekday5=weekday(day);
    day5=day;
    day=day-1;
  end;
  end;
run;
%mend;
%five;

我希望变量day5包含给定日期之前的第五个工作日。

2 个答案:

答案 0 :(得分:1)

我会尝试这样的事情:

/* Step back 5 days */
do i = 1 to 5;
  day - 1;
  /* Step back an extra day for each weekend or bank holiday encountered */
  do while(weekday(day) in (1, 7) or day in (&bhols.));
    day - 1;
  end;
end;

do i = 1 to 5将退回工作日,do while()将确保您跨过所有周末和银行假日,即使它们相邻。

答案 1 :(得分:0)

最简单的方式(而且我这么做)是找到所有不是周末和假期的日期。在值之前查找值5。

/*All weekdays in the year*/
data not_hols;
format date date9.;
do date="01JAN2015"d to "31DEC2015"d;
    if weekday(date) not in (6,7) then
        output;
end;
run;

/*Delete the holidays*/
proc sql noprint;
delete from not_hols
where date in (select bhols from bhols);
quit;

/*Add an index*/
data not_hols;
set not_hols;
index = _n_;
run;

/*Date to find*/
%let find_dt=07apr2015;

/*Look up the date based on the index being -5 from the value of
  find_dt */
proc sql noprint;
select date format date9.
    into :back5
from not_hols
where index = (
    select index-5 
    from not_hols 
    where date = "&find_dt"d
);
quit;

%put Back 5 from &find_dt = &back5;

创建NOT_HOLS数据的第一部分可以完成一次并保存在永久位置。然后,您可以在需要时使用SQL获取所需的日期。