BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Jariny
Calcite | Level 5

Hi all,

 

I am looking for a datastep which would rank rows based on few conditions.

Table contains customers and dates(of some action). columns = Customer_id and Date. 

I want to create ranking based on the condition - if difference between dates is higher than e.g. 50 then increase rank by 1.

If the difference between dates is lower than 50 then keep rank from previous row.

For each customer (one customer has several records with different dates)...

 

I have got this, where section is the desired ranking column.

 

%let day = 50;
data test (keep=Cust_ID date new section);
set atest.test ;
format new best12.;
format date1 date9.;
by cust_id ;
date1=lag((date)) ;
new=(date)-date1;
if first.cust_id then new=0 ;
if not first.cust_id and new=&day. then do;
section=1;
section = section +1;
end;
run;

 

Thanks for your help

J

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Not really sure what you are trying to achieve, but this should get you started 🙂

 

data have;
format Cust_ID $1. date date9.;
input Cust_ID $ Date: date9.;
datalines;
1 01Jan2015
1 01Feb2015
1 02Feb2015
1 02Jun2015
1 02Jul2015
1 20Jul2015
1 20Sep2015
1 20Nov2015
1 20Jan2016
1 20Apr2016
1 20May2016
2 01Jan2015
2 01Feb2015
2 02Feb2015
2 02Jun2015
2 02Jul2015
2 20Jul2015
2 20Sep2015
2 20Nov2015
2 20Jan2016
;

proc sort data = have;
	by cust_id;
run;

data want;
	set have;
	by cust_id;

	if first.cust_id then rank = 0;
	
	if (date - lag1(date)) > 50 then rank + 1;
	
	retain rank 0;
run;

 

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

Can you post some sample data please?

Jariny
Calcite | Level 5

is this format ok?

 

Cust_ID Date
1 01Jan2015
1 01Feb2015
1 02Feb2015
1 02Jun2015
1 02Jul2015
1 20Jul2015
1 20Sep2015
1 20Nov2015
1 20Jan2016
1 20Apr2016
1 20May2016
2 01Jan2015
2 01Feb2015
2 02Feb2015
2 02Jun2015
2 02Jul2015
2 20Jul2015
2 20Sep2015
2 20Nov2015
2 20Jan2016

PeterClemmensen
Tourmaline | Level 20

Not really sure what you are trying to achieve, but this should get you started 🙂

 

data have;
format Cust_ID $1. date date9.;
input Cust_ID $ Date: date9.;
datalines;
1 01Jan2015
1 01Feb2015
1 02Feb2015
1 02Jun2015
1 02Jul2015
1 20Jul2015
1 20Sep2015
1 20Nov2015
1 20Jan2016
1 20Apr2016
1 20May2016
2 01Jan2015
2 01Feb2015
2 02Feb2015
2 02Jun2015
2 02Jul2015
2 20Jul2015
2 20Sep2015
2 20Nov2015
2 20Jan2016
;

proc sort data = have;
	by cust_id;
run;

data want;
	set have;
	by cust_id;

	if first.cust_id then rank = 0;
	
	if (date - lag1(date)) > 50 then rank + 1;
	
	retain rank 0;
run;

 

Jariny
Calcite | Level 5

Thanks a lot for your help. Using this code I am getting the results (see in column Rank), but I would expect something like in Rank_new. Rank should increase when the difference between dates is higher than 50

Cust_ID Date Rank Rank_new Date_diff
1 01Jan2015 0 0 0
1 01Feb2015 1 1 31
1 02Feb2015 1 1 1
1 02Jun2015 2 2 120
1 02Jul2015 2 2 30
1 20Jul2015 2 2 18
1 20Sep2015 3 3 62
1 20Nov2015 3 4 61
1 20Jan2016 3 5 61
1 20Apr2016 4 6 91
1 20May2016 3 6 30
2 01Jan2015 0 0 0
2 01Feb2015 1 1 31
2 02Feb2015 1 1 1
2 02Jun2015 2 2 120
2 02Jul2015 2 2 30
2 20Jul2015 2 2 18
2 20Sep2015 3 3 62
2 20Nov2015 3 4 61
2 20Jan2016 3 5 61

PeterClemmensen
Tourmaline | Level 20

This seems odd to me. Between 01Jan2015 and 01Feb2015 there are no more than 50 days, yet you want rank to increase between your first two rows?

 

These are the results I am getting from running the code I provided:

 

Udklip.PNG

Jariny
Calcite | Level 5

It was apparently somehow stuck. It works now.

Great, thank you. 

J

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 2656 views
  • 1 like
  • 2 in conversation