SAS使用INTNX / INTCK将日期快进至限制

时间:2018-09-12 15:55:38

标签: sas floor ceil sas-studio

我正在寻找一个变量观测值的日期,并且基本上通过其指定的重新定价参数将其向前滚动,直到目标日期为止

正在使用的数据集是:

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个月的重新定价频率,直到它尽可能接近日期“ 31DEC2017”为止。预先感谢。

编辑,包括我最近的工作

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;

1 个答案:

答案 0 :(得分:1)

INTNX函数将计算一个递增的日期值,并允许指定结果的时间间隔对齐方式(在您的情况下,是月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;