<?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: How to select those observations for which the value of a variable keeps increasing and never lo in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-those-observations-for-which-the-value-of-a/m-p/709839#M218382</link>
    <description>Have updated the question.</description>
    <pubDate>Thu, 07 Jan 2021 07:32:04 GMT</pubDate>
    <dc:creator>Shradha1</dc:creator>
    <dc:date>2021-01-07T07:32:04Z</dc:date>
    <item>
      <title>How to select those observations for which the value of a variable keeps increasing and never lowers</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-those-observations-for-which-the-value-of-a/m-p/709821#M218372</link>
      <description>I have a list of customers and their monthly ratings ranging from 0 to 9 for all the months. I want to select only those customers for whom once the rating reached value 2 or above, it kept on increasing thereafter and never fell back to a lower rating(0 or 1), or stayed the same. In other words, I want those customers for whom their rating has been 2 or above, with monotonously increasing in the later months. I am using Sas 9.4.&lt;BR /&gt;&lt;BR /&gt;Data sample;&lt;BR /&gt;Input customer$8. Rating month date9.;&lt;BR /&gt;Cards;&lt;BR /&gt;A 0 Jan19&lt;BR /&gt;A 1 Feb19&lt;BR /&gt;A 2 Mar19&lt;BR /&gt;A 2 Apr19&lt;BR /&gt;A 3 May19&lt;BR /&gt;A 4 Jun19&lt;BR /&gt;B 2 Apr19&lt;BR /&gt;B 3 May19&lt;BR /&gt;B 4 Jun19&lt;BR /&gt;B 5 Jul19&lt;BR /&gt;B 6 Aug19&lt;BR /&gt;B 5 Sep19&lt;BR /&gt;B 4 Oct19&lt;BR /&gt;C 2 Jan19&lt;BR /&gt;C 2 Feb19&lt;BR /&gt;C 3 Mar19&lt;BR /&gt;C 4 Apr19&lt;BR /&gt;C 5 May 19&lt;BR /&gt;;&lt;BR /&gt;Run;&lt;BR /&gt;&lt;BR /&gt;As per requirement, I will want the result to contain customer A and Customer C</description>
      <pubDate>Thu, 07 Jan 2021 07:31:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-those-observations-for-which-the-value-of-a/m-p/709821#M218372</guid>
      <dc:creator>Shradha1</dc:creator>
      <dc:date>2021-01-07T07:31:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to select those observations for which the value of a variable keeps increasing and never lo</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-those-observations-for-which-the-value-of-a/m-p/709823#M218374</link>
      <description>&lt;P&gt;Can you show us a sample of your data please? makes it much easier to provide a usable code answer.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jan 2021 06:28:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-those-observations-for-which-the-value-of-a/m-p/709823#M218374</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-07T06:28:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to select those observations for which the value of a variable keeps increasing and never lo</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-those-observations-for-which-the-value-of-a/m-p/709830#M218376</link>
      <description>Hi! Have uploaded a short table with the question for sample</description>
      <pubDate>Thu, 07 Jan 2021 06:56:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-those-observations-for-which-the-value-of-a/m-p/709830#M218376</guid>
      <dc:creator>Shradha1</dc:creator>
      <dc:date>2021-01-07T06:56:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to select those observations for which the value of a variable keeps increasing and never lo</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-those-observations-for-which-the-value-of-a/m-p/709834#M218379</link>
      <description>&lt;P&gt;Please do not post your data as an image.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But.. what do you want the result to look like given this data?&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jan 2021 07:05:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-those-observations-for-which-the-value-of-a/m-p/709834#M218379</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-07T07:05:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to select those observations for which the value of a variable keeps increasing and never lo</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-those-observations-for-which-the-value-of-a/m-p/709835#M218380</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/361528"&gt;@Shradha1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hi! Have uploaded a short table with the question for sample&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Please post the data as data-step using datalines - so that we have something to work with. Also show the expected result using that dataset.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jan 2021 07:06:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-those-observations-for-which-the-value-of-a/m-p/709835#M218380</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2021-01-07T07:06:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to select those observations for which the value of a variable keeps increasing and never lo</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-those-observations-for-which-the-value-of-a/m-p/709837#M218381</link>
      <description>&lt;P&gt;Try next code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have; by customer month; run;
/* assumed month is a sas numeric date */

data want;
 set have;
   by customer; 
        retain max_rate out_flag;
        if first.customer then do;
           if rating ge 2 then out_flag=0; else flag=-1;
           max_rate = rating;
        end; 
        else do;
		   if flag ge 0 then and
		      rating ge max_rate then do;
			     out_flag=1;
				 max_rate=rating;
		end; end;
		if last.customer and out_flag ge 0;
run;
              		   &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Jan 2021 07:24:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-those-observations-for-which-the-value-of-a/m-p/709837#M218381</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2021-01-07T07:24:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to select those observations for which the value of a variable keeps increasing and never lo</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-those-observations-for-which-the-value-of-a/m-p/709839#M218382</link>
      <description>Have updated the question.</description>
      <pubDate>Thu, 07 Jan 2021 07:32:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-those-observations-for-which-the-value-of-a/m-p/709839#M218382</guid>
      <dc:creator>Shradha1</dc:creator>
      <dc:date>2021-01-07T07:32:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to select those observations for which the value of a variable keeps increasing and never lo</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-those-observations-for-which-the-value-of-a/m-p/709840#M218383</link>
      <description>Have updated the post</description>
      <pubDate>Thu, 07 Jan 2021 07:32:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-those-observations-for-which-the-value-of-a/m-p/709840#M218383</guid>
      <dc:creator>Shradha1</dc:creator>
      <dc:date>2021-01-07T07:32:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to select those observations for which the value of a variable keeps increasing and never lo</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-those-observations-for-which-the-value-of-a/m-p/709851#M218390</link>
      <description>&lt;P&gt;If you only want a single record with the customer id for the relevant customers, this should do it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do until(last.customer);
    set sample;
    by customer;
    dif=dif(rating);
    if first.customer then
      wanted=1;
    else if dif&amp;lt;0 then
      wanted=0;
    end;
  if wanted;
  keep customer;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want all the records for those customers, here's how:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do until(last.customer);
    set sample;
    by customer;
    dif=dif(rating);
    if first.customer then
      wanted=1;
    else if dif&amp;lt;0 then
      wanted=0;
    end;
  do until(last.customer);
    set sample;
    by customer;
    if wanted then 
      output;
    end;
  drop dif wanted;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Jan 2021 08:33:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-those-observations-for-which-the-value-of-a/m-p/709851#M218390</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2021-01-07T08:33:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to select those observations for which the value of a variable keeps increasing and never lo</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-those-observations-for-which-the-value-of-a/m-p/709852#M218391</link>
      <description>&lt;P&gt;Try this.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My logic is this. if the rating increases in the last observation by group compared to the second last and the rating in the last obs is &amp;gt; 2. Then I assume that the rating has already been equal to 2 and has been increasing from then.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See if it works for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data sample;
Input customer $ Rating month $;
Cards;
A 0 Jan19
A 1 Feb19
A 2 Mar19
A 2 Apr19
A 3 May19
A 4 Jun19
B 2 Apr19
B 3 May19
B 4 Jun19
B 5 Jul19
B 6 Aug19
B 5 Sep19
B 4 Oct19
C 2 Jan19
C 2 Feb19
C 3 Mar19
C 4 Apr19
C 5 May19
;

data want(drop = flag);
   do _N_ = 1 by 1 until (last.customer);
      set sample;
      by customer;
      flag = (dif(Rating) = 1 &amp;amp; Rating &amp;gt; 2);
   end;
   
   do _N_ = 1 to _N_;
      set sample;
      if flag then output;
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;customer  Rating  month 
A         0       Jan19 
A         1       Feb19 
A         2       Mar19 
A         2       Apr19 
A         3       May19 
A         4       Jun19 
C         2       Jan19 
C         2       Feb19 
C         3       Mar19 
C         4       Apr19 
C         5       May19 &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jan 2021 08:34:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-those-observations-for-which-the-value-of-a/m-p/709852#M218391</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-07T08:34:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to select those observations for which the value of a variable keeps increasing and never lo</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-those-observations-for-which-the-value-of-a/m-p/709859#M218397</link>
      <description>This was exactly what I was looking for. Thanks a lot!</description>
      <pubDate>Thu, 07 Jan 2021 09:25:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-those-observations-for-which-the-value-of-a/m-p/709859#M218397</guid>
      <dc:creator>Shradha1</dc:creator>
      <dc:date>2021-01-07T09:25:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to select those observations for which the value of a variable keeps increasing and never lo</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-those-observations-for-which-the-value-of-a/m-p/709862#M218399</link>
      <description>Just noticed that this code is missing cases like this:-&lt;BR /&gt;customer Rating month&lt;BR /&gt;A 0 Jan19&lt;BR /&gt;A 1 Feb19&lt;BR /&gt;A 2 Mar19&lt;BR /&gt;A 3 Apr19&lt;BR /&gt;A 3 May19&lt;BR /&gt;A 3 Jun19&lt;BR /&gt;&lt;BR /&gt;In this case customer A is being left out while using this code whereas it is wanted.</description>
      <pubDate>Thu, 07 Jan 2021 10:16:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-those-observations-for-which-the-value-of-a/m-p/709862#M218399</guid>
      <dc:creator>Shradha1</dc:creator>
      <dc:date>2021-01-07T10:16:50Z</dc:date>
    </item>
  </channel>
</rss>

