简体   繁体   中英

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. The result will be that consumer 1 contacted us 4 times from his first contact consecutive.

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.

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;

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.

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

as you can see in the SAS log the <> operator is wrongly interpreted as 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; , 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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