如何限制SAS中的月数

如何限制SAS中的月数,sas,base,proc,proc-sql,Sas,Base,Proc,Proc Sql,我有一个列名“month”,它有31个月 我想做的是限制月数 例如:-本月后接两个未来月份,前三个月后接本月 如果当前月份是4月,那么它必须显示 二月-三月-四月(当月)-五月-六月 任何人都知道,我们如何在SAS中做到这一点,它应该动态变化 当前表列月份(格式dtdate9.) 2016年1月1日 2016年2月1日 2016年3月1日 2016年4月1日 2016年5月1日 2016年6月1日 2016年7月1日 2016年8月1日 2016年9月1日 2016年10月1日 2016年11月

我有一个列名“month”,它有31个月

我想做的是限制月数

例如:-本月后接两个未来月份,前三个月后接本月

如果当前月份是4月,那么它必须显示

二月-三月-四月(当月)-五月-六月

任何人都知道,我们如何在SAS中做到这一点,它应该动态变化

当前表列月份(格式dtdate9.)

2016年1月1日 2016年2月1日 2016年3月1日 2016年4月1日 2016年5月1日 2016年6月1日 2016年7月1日 2016年8月1日 2016年9月1日 2016年10月1日 2016年11月1日 2016年12月1日 2017年1月1日 2017年2月1日 2017年3月1日 2017年4月1日 2017年5月1日 2017年6月1日 2017年7月1日 2017年8月1日 2017年9月1日 2017年10月1日 2017年11月1日 2017年12月1日 2018年1月1日 2018年2月1日 2018年3月1日 2018年4月1日 2018年5月1日 2018年6月1日 2018年12月1日

输出:-我需要两列max-date和min-date:max-date将给出+2,min-date将是-3,并且对于每个当前月份,它应该动态更改。我将在我的报告工具SAS VA中使用这两列max和min作为min和max之间的月份

proc sql;
create table want2 as
select * from sorted
where intck('month',date(),date) between 2 and -3 ;
quit;

提前感谢

如果您有表格数据,则只需执行两个步骤:

  • (可选步骤)按日期而不是月份名称对数据进行排序
    PROC Sort
  • 使用
    INTCK()
    函数检查所需的时间间隔(以月为单位)和返回今天日期的日期()
  • 虚拟数据:

    data have;
    length date 8. month $3. ;
    input date month $ ;
    informat date anydtdte20.;
    format date ddmmyys10.;
    datalines;
    01/07/2018 JUL
    01/02/2018 FEB
    01/03/2018 MAR
    01/04/2018 APR
    01/05/2018 MAY
    01/06/2018 JUN
    01/08/2018 AUG
    01/09/2018 SEP
    ;
    run;
    
    proc sort data=have out=sorted; by date; run;
    
    date=01/02/2018 month=FEB diff=-3 
    date=01/03/2018 month=MAR diff=-2 
    date=01/04/2018 month=APR diff=-1 
    date=01/05/2018 month=MAY diff=0 
    date=01/06/2018 month=JUN diff=1 
    date=01/07/2018 month=JUL diff=2
    
    data have;
    input date ;
    informat date date9.;
    datalines;
    01NOV2017 
    01DEC2017 
    01JAN2018 
    01FEB2018 
    01MAR2018 
    01APR2018 
    01MAY2018 
    01JUN2018 
    ;
    run;
    
    proc sql;
    create table want as
    select 
    date format=date9. ,
    intnx('month',date,-3) as min_date format=date9.,
    intnx('month',date,+2) as max_date format=date9.
    from have;
    quit;
    
    date=01NOV2017 min_date=01AUG2017 max_date=01JAN2018
    date=01DEC2017 min_date=01SEP2017 max_date=01FEB2018
    date=01JAN2018 min_date=01OCT2017 max_date=01MAR2018
    date=01FEB2018 min_date=01NOV2017 max_date=01APR2018
    date=01MAR2018 min_date=01DEC2017 max_date=01MAY2018
    date=01APR2018 min_date=01JAN2018 max_date=01JUN2018
    date=01MAY2018 min_date=01FEB2018 max_date=01JUL2018
    date=01JUN2018 min_date=01MAR2018 max_date=01AUG2018
    
    解决方案1:使用数据步骤

    data want;
    set sorted;
    diff= intck('month',date(),date);
    if (diff<= 2 and diff>= -3) then output;
    run;
    
    输出:

    data have;
    length date 8. month $3. ;
    input date month $ ;
    informat date anydtdte20.;
    format date ddmmyys10.;
    datalines;
    01/07/2018 JUL
    01/02/2018 FEB
    01/03/2018 MAR
    01/04/2018 APR
    01/05/2018 MAY
    01/06/2018 JUN
    01/08/2018 AUG
    01/09/2018 SEP
    ;
    run;
    
    proc sort data=have out=sorted; by date; run;
    
    date=01/02/2018 month=FEB diff=-3 
    date=01/03/2018 month=MAR diff=-2 
    date=01/04/2018 month=APR diff=-1 
    date=01/05/2018 month=MAY diff=0 
    date=01/06/2018 month=JUN diff=1 
    date=01/07/2018 month=JUL diff=2
    
    data have;
    input date ;
    informat date date9.;
    datalines;
    01NOV2017 
    01DEC2017 
    01JAN2018 
    01FEB2018 
    01MAR2018 
    01APR2018 
    01MAY2018 
    01JUN2018 
    ;
    run;
    
    proc sql;
    create table want as
    select 
    date format=date9. ,
    intnx('month',date,-3) as min_date format=date9.,
    intnx('month',date,+2) as max_date format=date9.
    from have;
    quit;
    
    date=01NOV2017 min_date=01AUG2017 max_date=01JAN2018
    date=01DEC2017 min_date=01SEP2017 max_date=01FEB2018
    date=01JAN2018 min_date=01OCT2017 max_date=01MAR2018
    date=01FEB2018 min_date=01NOV2017 max_date=01APR2018
    date=01MAR2018 min_date=01DEC2017 max_date=01MAY2018
    date=01APR2018 min_date=01JAN2018 max_date=01JUN2018
    date=01MAY2018 min_date=01FEB2018 max_date=01JUL2018
    date=01JUN2018 min_date=01MAR2018 max_date=01AUG2018
    
    使用
    INTNX()
    函数获取特定的数据范围(增量或减量)

    数据:

    data have;
    length date 8. month $3. ;
    input date month $ ;
    informat date anydtdte20.;
    format date ddmmyys10.;
    datalines;
    01/07/2018 JUL
    01/02/2018 FEB
    01/03/2018 MAR
    01/04/2018 APR
    01/05/2018 MAY
    01/06/2018 JUN
    01/08/2018 AUG
    01/09/2018 SEP
    ;
    run;
    
    proc sort data=have out=sorted; by date; run;
    
    date=01/02/2018 month=FEB diff=-3 
    date=01/03/2018 month=MAR diff=-2 
    date=01/04/2018 month=APR diff=-1 
    date=01/05/2018 month=MAY diff=0 
    date=01/06/2018 month=JUN diff=1 
    date=01/07/2018 month=JUL diff=2
    
    data have;
    input date ;
    informat date date9.;
    datalines;
    01NOV2017 
    01DEC2017 
    01JAN2018 
    01FEB2018 
    01MAR2018 
    01APR2018 
    01MAY2018 
    01JUN2018 
    ;
    run;
    
    proc sql;
    create table want as
    select 
    date format=date9. ,
    intnx('month',date,-3) as min_date format=date9.,
    intnx('month',date,+2) as max_date format=date9.
    from have;
    quit;
    
    date=01NOV2017 min_date=01AUG2017 max_date=01JAN2018
    date=01DEC2017 min_date=01SEP2017 max_date=01FEB2018
    date=01JAN2018 min_date=01OCT2017 max_date=01MAR2018
    date=01FEB2018 min_date=01NOV2017 max_date=01APR2018
    date=01MAR2018 min_date=01DEC2017 max_date=01MAY2018
    date=01APR2018 min_date=01JAN2018 max_date=01JUN2018
    date=01MAY2018 min_date=01FEB2018 max_date=01JUL2018
    date=01JUN2018 min_date=01MAR2018 max_date=01AUG2018
    
    解决方案:

    data have;
    length date 8. month $3. ;
    input date month $ ;
    informat date anydtdte20.;
    format date ddmmyys10.;
    datalines;
    01/07/2018 JUL
    01/02/2018 FEB
    01/03/2018 MAR
    01/04/2018 APR
    01/05/2018 MAY
    01/06/2018 JUN
    01/08/2018 AUG
    01/09/2018 SEP
    ;
    run;
    
    proc sort data=have out=sorted; by date; run;
    
    date=01/02/2018 month=FEB diff=-3 
    date=01/03/2018 month=MAR diff=-2 
    date=01/04/2018 month=APR diff=-1 
    date=01/05/2018 month=MAY diff=0 
    date=01/06/2018 month=JUN diff=1 
    date=01/07/2018 month=JUL diff=2
    
    data have;
    input date ;
    informat date date9.;
    datalines;
    01NOV2017 
    01DEC2017 
    01JAN2018 
    01FEB2018 
    01MAR2018 
    01APR2018 
    01MAY2018 
    01JUN2018 
    ;
    run;
    
    proc sql;
    create table want as
    select 
    date format=date9. ,
    intnx('month',date,-3) as min_date format=date9.,
    intnx('month',date,+2) as max_date format=date9.
    from have;
    quit;
    
    date=01NOV2017 min_date=01AUG2017 max_date=01JAN2018
    date=01DEC2017 min_date=01SEP2017 max_date=01FEB2018
    date=01JAN2018 min_date=01OCT2017 max_date=01MAR2018
    date=01FEB2018 min_date=01NOV2017 max_date=01APR2018
    date=01MAR2018 min_date=01DEC2017 max_date=01MAY2018
    date=01APR2018 min_date=01JAN2018 max_date=01JUN2018
    date=01MAY2018 min_date=01FEB2018 max_date=01JUL2018
    date=01JUN2018 min_date=01MAR2018 max_date=01AUG2018
    
    输出:

    data have;
    length date 8. month $3. ;
    input date month $ ;
    informat date anydtdte20.;
    format date ddmmyys10.;
    datalines;
    01/07/2018 JUL
    01/02/2018 FEB
    01/03/2018 MAR
    01/04/2018 APR
    01/05/2018 MAY
    01/06/2018 JUN
    01/08/2018 AUG
    01/09/2018 SEP
    ;
    run;
    
    proc sort data=have out=sorted; by date; run;
    
    date=01/02/2018 month=FEB diff=-3 
    date=01/03/2018 month=MAR diff=-2 
    date=01/04/2018 month=APR diff=-1 
    date=01/05/2018 month=MAY diff=0 
    date=01/06/2018 month=JUN diff=1 
    date=01/07/2018 month=JUL diff=2
    
    data have;
    input date ;
    informat date date9.;
    datalines;
    01NOV2017 
    01DEC2017 
    01JAN2018 
    01FEB2018 
    01MAR2018 
    01APR2018 
    01MAY2018 
    01JUN2018 
    ;
    run;
    
    proc sql;
    create table want as
    select 
    date format=date9. ,
    intnx('month',date,-3) as min_date format=date9.,
    intnx('month',date,+2) as max_date format=date9.
    from have;
    quit;
    
    date=01NOV2017 min_date=01AUG2017 max_date=01JAN2018
    date=01DEC2017 min_date=01SEP2017 max_date=01FEB2018
    date=01JAN2018 min_date=01OCT2017 max_date=01MAR2018
    date=01FEB2018 min_date=01NOV2017 max_date=01APR2018
    date=01MAR2018 min_date=01DEC2017 max_date=01MAY2018
    date=01APR2018 min_date=01JAN2018 max_date=01JUN2018
    date=01MAY2018 min_date=01FEB2018 max_date=01JUL2018
    date=01JUN2018 min_date=01MAR2018 max_date=01AUG2018
    

    请添加源表的示例以及您希望最终表的外观。除了您拥有和想要的数据示例之外,请添加您尝试过的代码。看到你解决问题的方法会帮助其他人。问题已编辑,请检查嗨,非常感谢你的回答,我想用不同的方式来做,比如:-我将计算两个字段max date+2和min date。-3然后我将使用它作为我的报告,如Month between(min&max),这很简单,请举例说明问题的输入和输出(表视图),我将更新我的答案。完成,请检查我添加了另一个答案您也可以使用VA Report Designer.ya中的计算字段在SAS VA中执行类似操作,我这样做了,但我的团队负责人想在数据查询中这样做。我这样做,但它给出了错误的“数据清理1_数据”;设置stag0(重命名=(度量月=月);公制月=卖出(月日,日期9);下降月份;跑proc-sql;创建表格时,选择month_dt format=date9,intnx('month',metric_month,-3)作为min_date format=date9,intnx('month',metricmonth,+2)作为max_date format=date9。来自clean1_数据;退出错误:函数INTNX需要一个数值表达式作为参数2。错误:字符表达式需要字符格式。公制月份的数据类型是什么?运行:
    procsql;描述表0;退出