Hello,
I have a dataset that looks like the following:
Have
Customer Total_Purchases
1 1
1 1
1 1
1 2
1 2
1 3
Want
Customer Total_Purchases New_Purchase
1 1 0
1 1 0
1 1 1
1 2 0
1 2 1
1 3 0
Essentially what I want to do is create a flag (New_Purchase) that indicates 1 if and only if the next row increments up by 1 and 0 otherwise. So in this case the New_Purchase variable will equal 1 as highlighted in the want dataset as an example.
Is there a way to do this?
You could use a "look-ahead". However, I note you haven't really told us what happens if the data switches to a different customer, and so you should really keep in mind that you need to provide us realistic examples.
I was going to produce code for a "look-ahead", but it would only work on your data which is for one customer, and so it would be relatively useless code. I think I'll wait until you address the above issue.
I'm afraid I don't understand what you want to do when you get a new customer. If total_purchases on the first row of customer 2 increment by 1 from the last row of customer 1, what do you want to do there?
Okay, so now we know what the data for two customers will not look like. Can you show us a realistic example of data for two customers and which flags you want where?
What happens in this case?
If 8 still gets a one the solution below should work.
Have
Customer Total_Purchases
1 1
1 1
1 1
1 2
1 2
1 8
For your example data this works but won't scale and is likely wrong.
data want;
set have;
by customer total_purchases;
if last.total_purchases then flag=1;
else flag=0;
run;
Here's how I see it. Assuming your data is in the proper order as you have presented it:
data want;
set have;
by customer total_purchases;
new_purchase = last.total_purchases and not last.customer;
run;
data have;
input Customer Total_Purchases ;
cards;
1 1
1 1
1 1
1 2
1 2
1 3
2 1
2 1
2 1
2 2
2 2
;
data want;
merge have have(keep=Customer Total_Purchases
rename=(Customer=_Customer Total_Purchases=_Total_Purchases) firstobs=2);
if Customer=_Customer and Total_Purchases+1=_Total_Purchases then New_Purchase=1;
else New_Purchase=0;
drop _: ;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.