Help using Base SAS procedures

Counting number of times the customer has been contacted: Count restart with 1 after each order

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

Counting number of times the customer has been contacted: Count restart with 1 after each order

Hi,I would appreciate your help. Here's what I am trying to do. I have a dataset that tells me customerID, contact date, order date. I want to find out how many times on averagecustomers  have been contacted before they place order.

Right now, I have my code as

data  contact1; set contact;

by accountid contactdate orderdate;

if first.accountid then do;

temp=0;

end;

temp+1;

run:

This code create a column call temp that keep counting 1,2,3,4... for each accountid. However, I have this count to reset once contactdate>orderdate. How do I do that?

Any help would be very much appreciated.

Thank you


Accepted Solutions
Solution
‎10-08-2014 01:41 PM
Trusted Advisor
Posts: 1,204

Re: Counting number of times the customer has been contacted: Count restart with 1 after each order

data want(drop=order);

set have;

by ID Contactdate Orderdate;

order=lag(orderdate);

format order mmddyy10.;

if first.id or Contactdate=Order+1 then Contact_no=0;

Contact_no+1;

run;

View solution in original post


All Replies
Trusted Advisor
Posts: 1,204

Re: Counting number of times the customer has been contacted: Count restart with 1 after each order

Better to have a sample data. Try this

data  contact1; set contact;

by accountid contactdate orderdate;

if first.accountid or contactdate>orderdate then do;

temp=0;

end;

temp+1;

run:

Occasional Contributor
Posts: 10

Re: Counting number of times the customer has been contacted: Count restart with 1 after each order

Hi,

Thank you so much for your reply. Here's the sample data.  When I did what you suggested, anything after row 6 below has Contact#=1.  I want it to recount again 1,2,3,4...  Is that possible?

IDContact dateOrderdateContact #
111111/2/20141/6/20141
111111/3/20141/6/20142
111111/4/20141/6/20143
111111/5/20141/6/20144
111111/6/20141/6/20145
111111/7/20141/6/20141
111111/8/20141/6/20142
111111/9/20141/6/20143
111111/10/20141/6/20144
111111/11/20141/6/20145
111111/15/20141/6/20146
111111/16/20141/6/20147
111111/17/20141/6/20148
111111/18/20141/6/20149
111111/19/20141/6/201410
111111/20/20141/6/201411
111111/21/20141/6/201412
111111/22/20141/6/201413
111111/23/20141/6/201414
Solution
‎10-08-2014 01:41 PM
Trusted Advisor
Posts: 1,204

Re: Counting number of times the customer has been contacted: Count restart with 1 after each order

data want(drop=order);

set have;

by ID Contactdate Orderdate;

order=lag(orderdate);

format order mmddyy10.;

if first.id or Contactdate=Order+1 then Contact_no=0;

Contact_no+1;

run;

Occasional Contributor
Posts: 10

Re: Counting number of times the customer has been contacted: Count restart with 1 after each order

Thank you so much!! I really appreciate it.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 208 views
  • 0 likes
  • 2 in conversation