<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Ranking rows by two variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Ranking-rows-by-two-variables/m-p/349029#M80906</link>
    <description>&lt;P&gt;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?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;These are&amp;nbsp;the results I am getting from running the code I provided:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;IMG title="Udklip.PNG" alt="Udklip.PNG" src="https://communities.sas.com/t5/image/serverpage/image-id/8263i599F0000C0B08BDE/image-size/medium?v=1.0&amp;amp;px=-1" border="0" /&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 11 Apr 2017 09:08:07 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2017-04-11T09:08:07Z</dc:date>
    <item>
      <title>Ranking rows by two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Ranking-rows-by-two-variables/m-p/349011#M80894</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am looking for a datastep which would rank rows based on few conditions.&lt;/P&gt;&lt;P&gt;Table contains customers and dates(of some action). columns = Customer_id and Date.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to create ranking based on the condition - if difference between dates is higher than e.g. 50 then increase rank by 1.&lt;/P&gt;&lt;P&gt;If the difference between dates is lower than 50 then keep rank from previous row.&lt;/P&gt;&lt;P&gt;For each customer (one customer has several records with different dates)...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have got this, where section is the desired ranking column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let day = 50;&lt;BR /&gt;data test (keep=Cust_ID date new section);&lt;BR /&gt;set atest.test ;&lt;BR /&gt;format new best12.;&lt;BR /&gt;format date1 date9.;&lt;BR /&gt;by cust_id ;&lt;BR /&gt;date1=lag((date)) ;&lt;BR /&gt;new=(date)-date1;&lt;BR /&gt;if first.cust_id then new=0 ;&lt;BR /&gt;if not first.cust_id and new=&amp;amp;day. then do;&lt;BR /&gt;section=1;&lt;BR /&gt;section = section +1;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your help&lt;/P&gt;&lt;P&gt;J&lt;/P&gt;</description>
      <pubDate>Tue, 11 Apr 2017 08:15:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Ranking-rows-by-two-variables/m-p/349011#M80894</guid>
      <dc:creator>Jariny</dc:creator>
      <dc:date>2017-04-11T08:15:03Z</dc:date>
    </item>
    <item>
      <title>Re: Ranking rows by two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Ranking-rows-by-two-variables/m-p/349013#M80896</link>
      <description>&lt;P&gt;Can you post some sample data please?&lt;/P&gt;</description>
      <pubDate>Tue, 11 Apr 2017 08:21:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Ranking-rows-by-two-variables/m-p/349013#M80896</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2017-04-11T08:21:19Z</dc:date>
    </item>
    <item>
      <title>Re: Ranking rows by two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Ranking-rows-by-two-variables/m-p/349015#M80898</link>
      <description>&lt;P&gt;is this format ok?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Cust_ID Date&lt;BR /&gt;1 01Jan2015&lt;BR /&gt;1 01Feb2015&lt;BR /&gt;1 02Feb2015&lt;BR /&gt;1 02Jun2015&lt;BR /&gt;1 02Jul2015&lt;BR /&gt;1 20Jul2015&lt;BR /&gt;1 20Sep2015&lt;BR /&gt;1 20Nov2015&lt;BR /&gt;1 20Jan2016&lt;BR /&gt;1 20Apr2016&lt;BR /&gt;1 20May2016&lt;BR /&gt;2 01Jan2015&lt;BR /&gt;2 01Feb2015&lt;BR /&gt;2 02Feb2015&lt;BR /&gt;2 02Jun2015&lt;BR /&gt;2 02Jul2015&lt;BR /&gt;2 20Jul2015&lt;BR /&gt;2 20Sep2015&lt;BR /&gt;2 20Nov2015&lt;BR /&gt;2 20Jan2016&lt;/P&gt;</description>
      <pubDate>Tue, 11 Apr 2017 08:30:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Ranking-rows-by-two-variables/m-p/349015#M80898</guid>
      <dc:creator>Jariny</dc:creator>
      <dc:date>2017-04-11T08:30:25Z</dc:date>
    </item>
    <item>
      <title>Re: Ranking rows by two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Ranking-rows-by-two-variables/m-p/349019#M80901</link>
      <description>&lt;P&gt;Not really sure what you are trying to achieve, but this should get you started &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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)) &amp;gt; 50 then rank + 1;
	
	retain rank 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Apr 2017 08:43:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Ranking-rows-by-two-variables/m-p/349019#M80901</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2017-04-11T08:43:04Z</dc:date>
    </item>
    <item>
      <title>Re: Ranking rows by two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Ranking-rows-by-two-variables/m-p/349025#M80904</link>
      <description>&lt;P&gt;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&lt;/P&gt;&lt;P&gt;Cust_ID Date Rank Rank_new Date_diff&lt;BR /&gt;1 01Jan2015 0 0 0&lt;BR /&gt;1 01Feb2015 1 1 31&lt;BR /&gt;1 02Feb2015 1 1 1&lt;BR /&gt;1 02Jun2015 2 2 120&lt;BR /&gt;1 02Jul2015 2 2 30&lt;BR /&gt;1 20Jul2015 2 2 18&lt;BR /&gt;1 20Sep2015 3 3 62&lt;BR /&gt;1 20Nov2015 3 4 61&lt;BR /&gt;1 20Jan2016 3 5 61&lt;BR /&gt;1 20Apr2016 4 6 91&lt;BR /&gt;1 20May2016 3 6 30&lt;BR /&gt;2 01Jan2015 0 0 0&lt;BR /&gt;2 01Feb2015 1 1 31&lt;BR /&gt;2 02Feb2015 1 1 1&lt;BR /&gt;2 02Jun2015 2 2 120&lt;BR /&gt;2 02Jul2015 2 2 30&lt;BR /&gt;2 20Jul2015 2 2 18&lt;BR /&gt;2 20Sep2015 3 3 62&lt;BR /&gt;2 20Nov2015 3 4 61&lt;BR /&gt;2 20Jan2016 3 5 61&lt;/P&gt;</description>
      <pubDate>Tue, 11 Apr 2017 08:59:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Ranking-rows-by-two-variables/m-p/349025#M80904</guid>
      <dc:creator>Jariny</dc:creator>
      <dc:date>2017-04-11T08:59:26Z</dc:date>
    </item>
    <item>
      <title>Re: Ranking rows by two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Ranking-rows-by-two-variables/m-p/349029#M80906</link>
      <description>&lt;P&gt;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?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;These are&amp;nbsp;the results I am getting from running the code I provided:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;IMG title="Udklip.PNG" alt="Udklip.PNG" src="https://communities.sas.com/t5/image/serverpage/image-id/8263i599F0000C0B08BDE/image-size/medium?v=1.0&amp;amp;px=-1" border="0" /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Apr 2017 09:08:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Ranking-rows-by-two-variables/m-p/349029#M80906</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2017-04-11T09:08:07Z</dc:date>
    </item>
    <item>
      <title>Re: Ranking rows by two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Ranking-rows-by-two-variables/m-p/349030#M80907</link>
      <description>&lt;P&gt;It was apparently somehow stuck. It works now.&lt;/P&gt;&lt;P&gt;Great, thank you.&amp;nbsp;&lt;/P&gt;&lt;P&gt;J&lt;/P&gt;</description>
      <pubDate>Tue, 11 Apr 2017 09:15:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Ranking-rows-by-two-variables/m-p/349030#M80907</guid>
      <dc:creator>Jariny</dc:creator>
      <dc:date>2017-04-11T09:15:55Z</dc:date>
    </item>
  </channel>
</rss>

