SAS 使用 INTNX/INTCK 快进日期直到限制

SAS Fast forward a date until a limit using INTNX/INTCK

我正在寻找可变观察日期,并根据其指定的重新定价参数继续向前滚动直到目标日期

正在使用的数据集是:

data have;
input repricing_frequency date_of_last_repricing end_date;
datalines;

3 15399 21367
10 12265 21367
15 13879 21367
;

format date_of_last_repricing end_date date9.;
informat date_of_last_repricing end_date date9.;
run;

所以我的想法是,我将继续对 date_of_last_repricing 应用 3 个月、10 个月或 15 个月的重新定价频率,直到它尽可能接近日期“2017 年 12 月 31 日” .提前致谢。

编辑包括我最近的作品:

data want;
set have;

repricing_N = intck('Month',date_of_last_repricing,'31DEC2017'd,'continuous');

dateoflastrepricing = intnx('Month',date_of_last_repricing,repricing_N,'E');

format dateoflastrepricing date9.;
informat dateoflastrepricing date9.;
run;

INTNX 函数将计算递增的日期值,并允许指定结果间隔对齐(在您的情况下,'end' 之后的第 n 个月)

data have;

  format   date_of_last_repricing end_date date9.;
  informat date_of_last_repricing end_date date9.;

  * use 12. to read the raw date values in the datalines;
  input repricing_frequency date_of_last_repricing: 12.  end_date: 12.;
datalines;
3 15399 21367
10 12265 21367
15 13879 21367
;
run;

data want;
  set have;

  status = 'Original';
  output;

  * increment and iterate;
  date_of_last_repricing = intnx('month',
      date_of_last_repricing, repricing_frequency, 'end'
  );
  do while (date_of_last_repricing <= end_date);
    status = 'Computed';
    output;
    date_of_last_repricing = intnx('month',
        date_of_last_repricing, repricing_frequency, 'end'
    );
  end;
run;

如果您只想计算最近的结束日期,如按重新定价频率迭代时,则不必迭代。您可以将月份除以频率以获得可​​能发生的迭代次数。

data want2;
  set have;

  nearest_end_month = intnx('month', end_date, 0, 'end');
  if nearest_end_month > end_date then nearest_end_month = intnx('month', nearest_end_month, -1, 'end');

  months_apart = intck('month', date_of_last_repricing, nearest_end_month);
  iterations_apart = floor(months_apart / repricing_frequency);

  iteration_months =  iterations_apart * repricing_frequency;

  nearest_end_date = intnx('month', date_of_last_repricing, iteration_months, 'end');

  format nearest: date9.;
run;

proc sql;
  select id, max(date_of_last_repricing) as nearest_end_date format=date9. from want group by id;
  select id, nearest_end_date from want2;
quit;