简体   繁体   English

自第一次通讯以来的连续月份数

[英]number of consecutive months from the first communication

I wouold like to ask you for help with this problem. 我想向您寻求有关此问题的帮助。

I have list of customers and for each customer I have list of months in which this customer contacted us. 我有客户清单,每个客户都有这个客户与我们联系的月份清单。 I need to know in how many months a particular customer contacted us. 我需要知道特定客户与我们联系了多少个月。 But there is problem that I need to know only number of consecutive months from his first contact. 但是有一个问题,我只需要知道他的第一次接触就可以连续几个月。

So I have table like this 所以我有这样的桌子

+---------+-------+
| cust id | month |
+---------+-------+
|       1 | 2     |
|       1 | 3     |
|       1 | 4     |
|       1 | 5     |
|       1 | 8     |
|       1 | 9     |
|       1 | 10    |
|       1 | 11    |
|       1 | 12    |
+---------+-------+

And I need to add column like this 我需要像这样添加列

+---------+-------+-------+
| cust id | month | flg   |
+---------+-------+-------+
|       1 | 2     | 1     |
|       1 | 3     | 1     |
|       1 | 4     | 1     |
|       1 | 5     | 1     |
|       1 | 8     | 0     |
|       1 | 9     | 0     |
|       1 | 10    | 0     |
|       1 | 11    | 0     |
|       1 | 12    | 0     |
+---------+-------+-------+

So finally I only sum all 1 in column flg. 所以最后我只将flg列中的所有1相加。 The result will be that consumer 1 contacted us 4 times from his first contact consecutive. 结果将是,消费者1从他的第一次联系开始就连续4次与我们联系。

I have tried use something like this but it does not work :( I do not know how to do that 1 will be only for fist consecutive line. 我试过使用这样的东西,但它不起作用:(我不知道该怎么做1仅适用于拳头连续线。

data test1;
 set customer_base;    
 retain month_ret;
 output;
 by cust_id month;
 month_ret = month;
 run;

 Data test2;
 Set test1;
 By cust_id;
 If first.cust_id then i=1;
 if month= month_ret+1 then i=1;
 if month<>month_ret+1 then output;
 Run;

Thank you very much 非常感谢你

There are two issues in your code. 您的代码中有两个问题。

1) if month= month_ret+1 then i=1; 1) if month= month_ret+1 then i=1;

every time that this is true, you set the flag to 1. However, you should only do this while this has never been false for the current customer. 每次为真时,都将标志设置为1。但是,只应这样做,而对于当前客户而言,这绝不是假的。

2) if month<>month_ret+1 then output; 2) if month<>month_ret+1 then output;

as you can see in the SAS log the <> operator is wrongly interpreted as MAX . 如您在SAS日志中看到的, <>运算符被错误地解释为MAX Therefore, this condition is never true. 因此,这种情况永远不会成立。 Furthermore, because you have an explicit output here, if this condition were to be correctly written as if month ne month_ret+1 then output; 此外,因为这里有一个显式output ,所以如果要正确地写出该条件,就if month ne month_ret+1 then output; , your resulting table would only contain records for which this condition is true. ,结果表将包含满足该条件的记录。

What you are trying to achieve can be done in one step, like so: 您想要实现的目标可以一步完成,如下所示:

   data have;
    infile datalines;
    input cust_id month;
    datalines;
    1 2
    1 3
    1 4
    1 5
    1 8
    1 9
    1 10
    1 11
    1 12
    ;
    run;

    data want (drop=p_month);
    set have;
    by cust_id;
    retain flg p_month;
    if first.cust_id then flg=1;
    else if month ne p_month+1 then flg=0;
    p_month=month;
    run;

For each first customer you set the flag to 1. Then whenever month is not equal to the previous plus one, you set the flag to 0. 对于每个第一个客户,将标志设置为1。然后,当月份不等于前一个加号时,将标志设置为0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM