Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Date 基本SAS:INTNX用于识别新客户和现有客户_Date_Sas - Fatal编程技术网

Date 基本SAS:INTNX用于识别新客户和现有客户

Date 基本SAS:INTNX用于识别新客户和现有客户,date,sas,Date,Sas,我试图在一个数据集中设置两个标志,一个用于新客户,另一个用于现有客户 新客户=具有上个月的原始购买日期 现有客户=上个月之前有原始购买日期 我想把过去18个月的所有活动都包括在内,从上个月的最后一天算起,上个月的最后一天是 %let end='30Jun15'd;run; 这是我到目前为止,我知道这是错误的代码 proc sql; create table Cust as select *, case when original_purchase_date between intnx('MO

我试图在一个数据集中设置两个标志,一个用于新客户,另一个用于现有客户

新客户=具有上个月的原始购买日期

现有客户=上个月之前有原始购买日期

我想把过去18个月的所有活动都包括在内,从上个月的最后一天算起,上个月的最后一天是

%let end='30Jun15'd;run;
这是我到目前为止,我知道这是错误的代码

proc sql;
create table Cust as
select
*,
case when original_purchase_date between  intnx('MONTH', &end, -1)  and intnx('MONTH', &end) then 1  else 0 end as new,
case when original_purchase_date lt intnx('MONTH', &end, -1)   then 1 
else 0 end as Existing
from orders
where original_purchase_date between intnx('MONTH', &end, -1) and intnx('MONTH', &end, -1) -18;
quit;

纠正你的错误,我来

select
    *,
    original_purchase_date between  intnx('MONTH', &end, 0)  and &end as new,
    original_purchase_date lt intnx('MONTH', &end, 0) as Existing
from orders
where original_purchase_date between intnx('MONTH', &end, -18) and &end;
intnx('MONTH',&end,0)
是您在
%let end=…
中指定的月份的开始,
intnx('MONTH',&end,-18)
是它之前的第18个月的开始

你可以用它做得更好

select
    *,
    original_purchase_date between  intnx('MONTH', Date(), -1, 'B')  and intnx('MONTH', Date(), -1, 'E') as new,
    original_purchase_date lt intnx('MONTH', Date(), -1, 'B') as Existing
from orders
where original_purchase_date between intnx('MONTH', Date(), -19, 'B') and intnx('MONTH', Date(), -1, 'E');

此处Date()是今天的日期(在&end之后的一个月中的日期),intnx的第四个参数指定是要开始还是结束这个月。

%let end='2015年6月30日;(没有“run;”,最好写yyyy几年)你知道不正确的代码是什么?在SAS中,布尔表达式有一个数字结果,如果表达式为真,则等于1,如果表达式为假,则等于0,因此你只需将intnx('MONTH',&end,-1)和intnx('MONTH',&end)之间的
原始购买日期作为新的
,如果没有
case
clause,函数intnx默认为时段的第一天,因此
intnx('MONTH',end,-1)
等于'2015年5月1日'd和
intnx('MONTH',end)
等于'2015年6月1日'd。您实际上需要'01Jun15'd和'30Jul15'在where子句中,您指定的起始日期晚于截止日期。此外,从日期中减去一个数字实际上是减去天数,而不是月份。