DATA Step, Macro, Functions and more

Ranking rows by two variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Ranking rows by two variables

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


Accepted Solutions
Solution
‎04-11-2017 05:16 AM
PROC Star
Posts: 547

Re: Ranking rows by two variables

[ Edited ]

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

 

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


All Replies
PROC Star
Posts: 547

Re: Ranking rows by two variables

Can you post some sample data please?

Occasional Contributor
Posts: 5

Re: Ranking rows by two variables

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

Solution
‎04-11-2017 05:16 AM
PROC Star
Posts: 547

Re: Ranking rows by two variables

[ Edited ]

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

 

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;

 

Occasional Contributor
Posts: 5

Re: Ranking rows by two variables

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

PROC Star
Posts: 547

Re: Ranking rows by two variables

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

Occasional Contributor
Posts: 5

Re: Ranking rows by two variables

It was apparently somehow stuck. It works now.

Great, thank you. 

J

☑ This topic is SOLVED.

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

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