<?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: Identify similar (negative) values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Identify-similar-negative-values/m-p/703386#M215504</link>
    <description>Please provide more details on the logic you want to apply&lt;BR /&gt;I don't understand what the logic is for setting flag=1 or the negative payments</description>
    <pubDate>Thu, 03 Dec 2020 15:01:19 GMT</pubDate>
    <dc:creator>AMSAS</dc:creator>
    <dc:date>2020-12-03T15:01:19Z</dc:date>
    <item>
      <title>Identify similar (negative) values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-similar-negative-values/m-p/703373#M215496</link>
      <description>&lt;P&gt;I am trying to identify credited amounts in a data set and changing their payment by negating their similar values previously displayed in a column.&lt;/P&gt;&lt;P&gt;Did tried hash objects but was unable to make it work. Can you please help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Have:&lt;/STRONG&gt;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Amount&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;Payment&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;243&lt;/TD&gt;&lt;TD&gt;600&lt;/TD&gt;&lt;TD&gt;40&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;243&lt;/TD&gt;&lt;TD&gt;800&lt;/TD&gt;&lt;TD&gt;60&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;243&lt;/TD&gt;&lt;TD&gt;400&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;/TR&gt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t;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;-50&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Thu, 03 Dec 2020 14:17:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-similar-negative-values/m-p/703373#M215496</guid>
      <dc:creator>kashun</dc:creator>
      <dc:date>2020-12-03T14:17:05Z</dc:date>
    </item>
    <item>
      <title>Re: Identify similar (negative) values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-similar-negative-values/m-p/703386#M215504</link>
      <description>Please provide more details on the logic you want to apply&lt;BR /&gt;I don't understand what the logic is for setting flag=1 or the negative payments</description>
      <pubDate>Thu, 03 Dec 2020 15:01:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-similar-negative-values/m-p/703386#M215504</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2020-12-03T15:01:19Z</dc:date>
    </item>
    <item>
      <title>Re: Identify similar (negative) values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-similar-negative-values/m-p/703397#M215509</link>
      <description>The flag means a corresponded value has been identified with its credited value (i.e. negative value ).</description>
      <pubDate>Thu, 03 Dec 2020 15:15:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-similar-negative-values/m-p/703397#M215509</guid>
      <dc:creator>kashun</dc:creator>
      <dc:date>2020-12-03T15:15:37Z</dc:date>
    </item>
    <item>
      <title>Re: Identify similar (negative) values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-similar-negative-values/m-p/703552#M215590</link>
      <description>&lt;P&gt;I assume logic is Group by id and flip positive payment value to minus when amount is minus and absolute values are equal.&lt;BR /&gt;Below program is done accordingly but performance may be slow.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dlm="09"x;
input ID Amount Payment;
datalines;
243	600	40
243	800	60
243	400	20
243	200	70
243	-600	0
243	-300	0
145	250	50
145	300	20
145	680	70
145	-500	0
145	500	50
134	780	20
134	400	30
134	450	50
134	200	10
134	-450	0
;
run;

data want;
  if _N_=1 then do;
    if 0 then set have;
    dcl hash h(multidata:"y") ;
    h.definekey("id","amount");
    h.definedata("amount","payment");
    h.definedone();
  end;
  do until(last);
    set have end=last;
    by id notsorted;
    if sign(amount)=-1 then _iorc_=h.add(key:id,key:abs(amount),data:abs(amount),data:abs(payment));
  end;
  if last;
  do until(last1);
    set have end=last1;
    by id notsorted;
    _payment=payment;
    if sign(amount)=1 and (h.check(key:id,key:abs(amount)) = 0) then do;
        _iorc_=h.replace(key:id,key:abs(amount),data:abs(amount),data:max(abs(payment),abs(_payment)));
      end;
  end;
  if last1;
  do until(last2);
    set have end=last2;
    by id notsorted;
    _payment=payment;
    _amount=amount;
    flag=(h.find(key:id,key:abs(_amount)) =0 and payment ne 0);
    if sign(_amount)=-1 then payment=-payment;
    output;
  end;	
  drop amount _payment;
  rename _amount=amount;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 Dec 2020 05:32:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-similar-negative-values/m-p/703552#M215590</guid>
      <dc:creator>hhinohar</dc:creator>
      <dc:date>2020-12-04T05:32:30Z</dc:date>
    </item>
    <item>
      <title>Re: Identify similar (negative) values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-similar-negative-values/m-p/703561#M215597</link>
      <description>&lt;P&gt;Is it necessary to maintain the original order of observations?&lt;/P&gt;
&lt;P&gt;If not try:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
   select Id, Amount, count(*)-1 as Flag, Payment
      from have
      group by id, abs(Amount)
   ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Assuming that for each ID and abs(Amount) only one or two observations exist.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF6600"&gt;&lt;STRONG&gt;NOTE: Wait, that step is to simple, of course, because it doesn't look for positive amount and a negative amount, two positive/negative will also be flagged. Sorry.&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Dec 2020 07:25:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-similar-negative-values/m-p/703561#M215597</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-12-04T07:25:23Z</dc:date>
    </item>
    <item>
      <title>Re: Identify similar (negative) values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-similar-negative-values/m-p/703563#M215598</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76157"&gt;@kashun&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I am trying to identify credited amounts in a data set and changing their payment by negating their similar values previously displayed in a column.&lt;/P&gt;
&lt;P&gt;Did tried hash objects but was unable to make it work. Can you please help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Helping you would be a lot easier, if you would post the code you have.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The following step seems to do what you expect, i am not sure if all special-cases are properly handled, because you have none in the example you have posted. In the future, please post data in usable form.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;

   if _n_ = 1 then do;
      declare hash h(dataset: 'work.have(drop=Payment');
      h.defineKey('Id', 'Amount');
      h.defineDone();
   end;

   Amount = Amount * -1; 
   Flag = h.check() = 0; /* check returns 0 if they key-pair is found */
   Amount = Amount * -1; /* reverting to the original value */
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 Dec 2020 07:37:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-similar-negative-values/m-p/703563#M215598</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-12-04T07:37:58Z</dc:date>
    </item>
    <item>
      <title>Re: Identify similar (negative) values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-similar-negative-values/m-p/703869#M215713</link>
      <description>Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;. Thanks very much for the input. Yes the order of the observations  should be maintained</description>
      <pubDate>Sat, 05 Dec 2020 18:57:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-similar-negative-values/m-p/703869#M215713</guid>
      <dc:creator>kashun</dc:creator>
      <dc:date>2020-12-05T18:57:36Z</dc:date>
    </item>
    <item>
      <title>Re: Identify similar (negative) values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-similar-negative-values/m-p/703871#M215714</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;. It seems this provides the flags and the proc sql deals with the payment calculation. I will add the data next time to make it easier. Thanks vert much.</description>
      <pubDate>Sat, 05 Dec 2020 19:00:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-similar-negative-values/m-p/703871#M215714</guid>
      <dc:creator>kashun</dc:creator>
      <dc:date>2020-12-05T19:00:03Z</dc:date>
    </item>
    <item>
      <title>Re: Identify similar (negative) values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identify-similar-negative-values/m-p/703889#M215729</link>
      <description>&lt;P&gt;After all, this is much cleaner.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  if _N_=1 then do;
    if 0 then set have;
    dcl hash h(dataset:"have");
    h.definekey("id","amount");
    h.definedata("payment");
    h.definedone();
  end;
    set have;
    by id notsorted;
    *keep original payment value;
    _payment=payment;
    *find negative amount key;
    flag=(h.find(key:id,key:-amount)=0);
    *fill payment value;
    payment=ifn(sign(amount)=-1,-payment,_payment);
  drop _payment;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 05 Dec 2020 22:33:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identify-similar-negative-values/m-p/703889#M215729</guid>
      <dc:creator>hhinohar</dc:creator>
      <dc:date>2020-12-05T22:33:47Z</dc:date>
    </item>
  </channel>
</rss>

