【问题标题】:create a flag for each month policy is active为每个月创建一个标志策略处于活动状态
【发布时间】:2021-09-14 09:24:26
【问题描述】:

我有一个政策日期从 2018 年 1 月 1 日到 2019 年 12 月 31 日(2 年)之间开始的数据集,其中包含以下信息:

policy          policy_beg_date    policy_end_date
a                 01-01-2018         06-02-2018
b                 04-02-2019         02-04-2020
c                 23-12-2019         03-02-2020
d                 02-02-2019    

政策开始日期(政策开始日期)和政策结束日期(政策结束日期 - 如果政策仍然有效,则缺失)

我想创建 13 个月的标志(flag_0month、flag_1month、....flag_13month),如果政策在第 0 个月、第 1 个月等处于活动状态,我给出 1.... 其中第 0 个月是beginnig,month1 是开始的月份 + 1 等等,所以我会有这样的东西:

policy          policy_beg_date    policy_end_date  month_begining   month_end  act_month0  act_month1  act_month2
a               01-01-2018           06-01-2018       201801          201801       1              
b               04-02-2019           02-04-2020       201902          202003       1              1          1
c               23-12-2019           03-02-2020       201912          202002       1              1          1
d               02-02-2019                            201902                       1              1          1

谁能帮我实现这个?

我已经在这篇文章中尝试过类似的东西:https://communities.sas.com/t5/SAS-Programming/How-to-create-a-flag-for-each-month-lying-between-2-d..。但我得到了错误,我认为这与我需要的不一样。

谢谢!!

【问题讨论】:

    标签: sas sas-macro


    【解决方案1】:
    data have ;
      infile cards truncover ;
      input policy : $1. policy_beg_date : ddmmyy10. policy_end_date : ddmmyy10. ;
      format policy_beg_date policy_end_date date9. ;
    datalines ;
    a 01-01-2018 06-02-2018
    b 04-02-2019 02-04-2020
    c 23-12-2019 03-02-2020
    d 02-02-2019    
    e 01-01-2020 31-01-2020
    ;
    run;
    
    data want ;
      set have ;
      /* use intck() to get # months */  
      months = intck('month',policy_beg_date,min(policy_end_date,date())) ;
      
      /* array to hold the flags */
      array act{0:13} act_month0-act_month13 ;
    
      /* loop from 0 to months, set the array flags */
      do mn = 0 to min(months,hbound(act)) ;
        act{mn} = 1 ;
      end ;
    run ;
    
    proc print noobs ; run ;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多