<?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 how do i retaining values by id in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-do-i-retaining-values-by-id/m-p/574331#M162264</link>
    <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to produce 3 sets under the following conditions. How could this be done?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
 input id amt;
 datalines;
 1 5
 1 5
 1 106
 2 90
 2 100
 3 111
 3 111
 3 111
 3 111
 4 837
 4 838
 4 839
 ;
run;

data want01; /*01 after the first two occurrences, the difference is greater than 20% compared to the third one*/
 input id flg$;
 datalines;
 1 Y
 ;
run;
data want02; /*02 where the amt field is constant*/
 input id flg$;
 datalines;
 3 Y
 ;
run;
data want03; /*03 where the amt field always changeing*/
 input id flg$;
 datalines;
 2 Y
 4 Y
 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 17 Jul 2019 19:06:16 GMT</pubDate>
    <dc:creator>ger15xxhcker</dc:creator>
    <dc:date>2019-07-17T19:06:16Z</dc:date>
    <item>
      <title>how do i retaining values by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-i-retaining-values-by-id/m-p/574331#M162264</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to produce 3 sets under the following conditions. How could this be done?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
 input id amt;
 datalines;
 1 5
 1 5
 1 106
 2 90
 2 100
 3 111
 3 111
 3 111
 3 111
 4 837
 4 838
 4 839
 ;
run;

data want01; /*01 after the first two occurrences, the difference is greater than 20% compared to the third one*/
 input id flg$;
 datalines;
 1 Y
 ;
run;
data want02; /*02 where the amt field is constant*/
 input id flg$;
 datalines;
 3 Y
 ;
run;
data want03; /*03 where the amt field always changeing*/
 input id flg$;
 datalines;
 2 Y
 4 Y
 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 17 Jul 2019 19:06:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-i-retaining-values-by-id/m-p/574331#M162264</guid>
      <dc:creator>ger15xxhcker</dc:creator>
      <dc:date>2019-07-17T19:06:16Z</dc:date>
    </item>
    <item>
      <title>Re: how do i retaining values by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-i-retaining-values-by-id/m-p/574435#M162320</link>
      <description>&lt;P&gt;What does this mean?&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token comment"&gt; after the first two occurrences, the difference is greater than 20% compared to the third one&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Jul 2019 03:22:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-i-retaining-values-by-id/m-p/574435#M162320</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-07-18T03:22:31Z</dc:date>
    </item>
    <item>
      <title>Re: how do i retaining values by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-i-retaining-values-by-id/m-p/574444#M162325</link>
      <description>&lt;P&gt;I'm sure there are data step approaches but I work with RDBMS's so much that I often think in SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your real datasets are huge this might not perform well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* source data ;
data have;
 input id amt;
 datalines;
 1 5
 1 5
 1 106
 2 90
 2 100
 3 111
 3 111
 3 111
 3 111
 4 837
 4 838
 4 839
 ;
run;

* create two keys - this could be a view if your data is large ;
data have;
   format sk gk;
   sk+1;
   set have;
   by id;
   if first.id then gk=0;
   gk+1;
run;

/*01 after the first two occurrences, the difference is greater than 20% compared to the third one*/
* this is unclear but here is a start ;
proc sql _method;
   * for debugging ;
   create table want1_debug as
   select a.id, a.amt, b.amt as amt_3
   from have a
   left join have b
   on a.id=b.id
   and b.gk=3
   order by a.sk;

   * for real ;
   create table want1 as
   select distinct a.id
   from have a
   left join have b
   on a.id=b.id
   and b.gk=3
   where b.amt &amp;gt; a.amt*1.2;
quit;

/*02 where the amt field is constant*/
proc sql _method;
   * for debugging ;
   create table want2_debug as
   select id,min(amt) as min_amt,max(amt) as max_amt
   from have
   group by id;

   * for real ;
   create table want2 as
   select distinct id 
   from (
   select id,min(amt) as min_amt,max(amt) as max_amt
   from have
   group by id
   ) x
   where x.min_amt = x.max_amt;
quit;

/*03 where the amt field always changeing*/
proc sql _method;
   * for debugging ;
   create table want3_debug as
   select a.id,a.amt,b.amt as next_amt,(a.amt ^= b.amt or b.amt is missing) as flag
   from have a
   left join have b
   on a.id=b.id
   and a.sk+1=b.sk
   order by a.sk;

   * for real ;
   create table want3 as
   select distinct id
   from (
   select id,min(flag) as min_flag 
   from (
   select a.id,a.amt,b.amt as next_amt,(a.amt ^= b.amt or b.amt is missing) as flag
   from have a
   left join have b
   on a.id=b.id
   and a.sk+1=b.sk
   ) x
   group by id
   having min_flag ^= 0
   ) x;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Jul 2019 04:30:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-i-retaining-values-by-id/m-p/574444#M162325</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-07-18T04:30:30Z</dc:date>
    </item>
    <item>
      <title>Re: how do i retaining values by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-i-retaining-values-by-id/m-p/574453#M162330</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Sorry for the wrong wording. An example:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1. row id =1 amt = 10 &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2. row id =1 amt = 11 &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;3. row id =1 amt = 13 (this one is 18,12% higher then the second one)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;4. row id=2&amp;nbsp; amt = 10&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;5. row id=2&amp;nbsp; amt = 11&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;6. row id=2&amp;nbsp; amt = 14 this third one is 27,2% higher then the second one (which is for&amp;nbsp;id =&amp;nbsp;2)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;i'd like to compare this values. If the third one is &amp;gt;= 20% then take a flag.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks for your help!&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jul 2019 05:48:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-i-retaining-values-by-id/m-p/574453#M162330</guid>
      <dc:creator>ger15xxhcker</dc:creator>
      <dc:date>2019-07-18T05:48:53Z</dc:date>
    </item>
    <item>
      <title>Re: how do i retaining values by id</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-do-i-retaining-values-by-id/m-p/574465#M162331</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Thank, you are right full of name conventions next time i will do so&lt;/SPAN&gt;! Your code is perfect for me. Only the first method need some changes. How can i do if I &lt;STRONG&gt;only&lt;/STRONG&gt; look at the&amp;nbsp; more then 20% &lt;STRONG&gt;increase&lt;/STRONG&gt;, not the &lt;SPAN&gt;decrease. So i modify my question: If the first 2 record is the same and the third one is more then 20 increase.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I really appreciate it!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jul 2019 06:49:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-do-i-retaining-values-by-id/m-p/574465#M162331</guid>
      <dc:creator>ger15xxhcker</dc:creator>
      <dc:date>2019-07-18T06:49:04Z</dc:date>
    </item>
  </channel>
</rss>

