<?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: Removing negative values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Removing-negative-values/m-p/617188#M180808</link>
    <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/296078"&gt;@Solly7&lt;/a&gt;&amp;nbsp; Will this help?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
   Data Have        ;                                 
input policy_no    amount    ;
cards; 
1                -500                                     
1                -500                                
1                 500                                                             
2                -1000                                  
3                -700
3                 700  
;

data want;
 if _n_=1 then do;
   dcl hash H (multidata:'y') ;
   h.definekey  ("amount") ;
   h.definedone () ;
 end;
 do _n_=1 by 1 until(last.policy_no);
  set have;
  by policy_no;
  if sign(amount)=1 then _iorc_=h.add();
 end;
 do _n_=1 to _n_;
  set have;
  if sign(amount)=-1 and h.check(key:abs(amount)) then continue;
  if sign(amount)=-1 and h.check(key:abs(amount))=0 then h.removedup(key:abs(amount));
  output;
 end;
 h.clear();
run;

 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;
&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 14 Jan 2020 13:19:09 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2020-01-14T13:19:09Z</dc:date>
    <item>
      <title>Removing negative values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-negative-values/m-p/617183#M180804</link>
      <description>&lt;P&gt;Hi I need help with below, i need to remove rows for policies which have negative amount(i.e $500) but with no corresponding positive amount(i.e $500). I also need to remove&amp;nbsp; 1 negative transaction from policies that have 2 negative transactions(same amount, i.e $500) and positive transaction($500). keep those transactions with one negative transaction($700) and one positive tranasaction(i.e $700&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; data.transactions&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;Data Have&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Data Want&lt;/P&gt;&lt;P&gt;policy no&amp;nbsp; &amp;nbsp; amount&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;policy no&amp;nbsp; &amp;nbsp; amount&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; -500&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; -500&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; -500&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;500&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;500&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; -700&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; -1000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;700&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; -700&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;700&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2020 12:55:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-negative-values/m-p/617183#M180804</guid>
      <dc:creator>Solly7</dc:creator>
      <dc:date>2020-01-14T12:55:37Z</dc:date>
    </item>
    <item>
      <title>Re: Removing negative values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-negative-values/m-p/617188#M180808</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/296078"&gt;@Solly7&lt;/a&gt;&amp;nbsp; Will this help?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
   Data Have        ;                                 
input policy_no    amount    ;
cards; 
1                -500                                     
1                -500                                
1                 500                                                             
2                -1000                                  
3                -700
3                 700  
;

data want;
 if _n_=1 then do;
   dcl hash H (multidata:'y') ;
   h.definekey  ("amount") ;
   h.definedone () ;
 end;
 do _n_=1 by 1 until(last.policy_no);
  set have;
  by policy_no;
  if sign(amount)=1 then _iorc_=h.add();
 end;
 do _n_=1 to _n_;
  set have;
  if sign(amount)=-1 and h.check(key:abs(amount)) then continue;
  if sign(amount)=-1 and h.check(key:abs(amount))=0 then h.removedup(key:abs(amount));
  output;
 end;
 h.clear();
run;

 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;
&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2020 13:19:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-negative-values/m-p/617188#M180808</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-14T13:19:09Z</dc:date>
    </item>
    <item>
      <title>Re: Removing negative values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-negative-values/m-p/617306#M180867</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/296078"&gt;@Solly7&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an alternative method using proc sql, probably not as efficient as the one provided by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It depends whether you're familiar with hash object.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	create table pos as
	select distinct policy_no, amount as pos_amount
	from have
	where amount &amp;gt; 0
	order by policy_no, amount;

	create table neg as
	select distinct policy_no, amount as neg_amount
	from have
	where amount &amp;lt; 0
	order by policy_no, amount;
quit;

proc sql;
	create table have2 as
	select a.policy_no, a.pos_amount, b.neg_amount
	from pos as a inner join neg as b
	on a.policy_no = b.policy_no and a.pos_amount = abs(b.neg_amount);
quit;

proc sql;
	create table want as
	select policy_no, pos_amount as amount
	from have2
	union
	select policy_no, neg_amount as amount
	from have2
	order by policy_no, abs(amount);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2020 20:29:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-negative-values/m-p/617306#M180867</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-01-14T20:29:00Z</dc:date>
    </item>
    <item>
      <title>Re: Removing negative values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-negative-values/m-p/617356#M180899</link>
      <description>&lt;P&gt;I have re-framed your task to: for each positive, keep it and a single matched negative:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Have ;                                 
  input policy_no    amount    ;
cards; 
1                -500
1                -500
1                 500
2                -1000
3                -700
3                 700
run;

data want (drop=_p);
  set have (where=(amount&amp;gt;0) in=inpos)
      have (in=infull);
  by policy_no;
  array pos_vals {20} _temporary_;

  if first.policy_no then call missing(of pos_vals{*});
  if inpos then pos_vals{1+n(of pos_vals{*})}=amount;
  if infull;

  if amount&amp;lt;0 then _p=whichn(-1*amount,of pos_vals{*});
  if _p&amp;gt;0 or amount&amp;gt;0 then output;
  if _p&amp;gt;0 then pos_vals{_p}=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For each policy_no, this reads all the positive, followed by all the positives and negatives.&amp;nbsp; In the first pass of positives, add to a&amp;nbsp; list of positive values.&amp;nbsp; In the second pass, when a negative value is encountered, see if it is matched in the list of positives.&amp;nbsp; If so keep it and delete the corresponding positive entry in the list.&amp;nbsp; Any negative not so matched is not output.&amp;nbsp; Also keep all positives.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2020 01:15:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-negative-values/m-p/617356#M180899</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-01-15T01:15:52Z</dc:date>
    </item>
    <item>
      <title>Re: Removing negative values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-negative-values/m-p/617369#M180902</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/296078"&gt;@Solly7&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;Methinks the simplest thing logically is:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Store the records with the positive amounts in a search table by policy and amount&lt;/LI&gt;
&lt;LI&gt;Read the file one record at a time.&lt;/LI&gt;
&lt;LI&gt;If amount is positive, output the record.&lt;/LI&gt;
&lt;LI&gt;Otherwise see if the absolute value of amount for the current value of policy is in the table. If it is, output the record and remove the respective item from the table to prevent another output for the same policy and negative amount downstream.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Extra advantages of the approach (besides the logical):&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;it works regardless of the input file's record order (sorted or disordered)&lt;/LI&gt;
&lt;LI&gt;it requires less than 2 passes through the input data&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;.&amp;nbsp;In SAS words: &amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                                                                             
  input policy_no amount ;                                                                                                              
  cards ;                                                                                                                               
3   -700                                                                                                                                
1    500                                                                                                                                
2  -1000                                                                                                                                
1   -500                                                                                                                                
3    700                                                                                                                                
1   -500                                                                                                                                
run ;                                                                                                                                   
                                                                                                                                        
data want ;                                                                                                                             
  if _n_ = 1 then do ;                                                                                                                  
    dcl hash h (dataset:"have (where = (amount &amp;gt; 0))") ;                                                                                
    h.definekey ("policy_no", "amount") ;                                                                                               
    h.definedone () ;                                                                                                                   
  end ;                                                                                                                                 
  set have ;                                                                                                                            
  if amount &amp;gt; 0 then output ;                                                                                                           
  else if h.check (key:policy_no, key:-amount) = 0 then do ;                                                                            
    output ;                                                                                                                            
    h.remove (key:policy_no, key:-amount) ;                                                                                             
  end ;                                                                                                                                 
run ;                   
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2020 04:25:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-negative-values/m-p/617369#M180902</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2020-01-15T04:25:47Z</dc:date>
    </item>
    <item>
      <title>Re: Removing negative values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-negative-values/m-p/617374#M180904</link>
      <description>&lt;PRE&gt;
 Data Have        ;                                 
input policy_no    amount    ;
cards; 
1                -500                                     
1                -500                                
1                 500                                                             
2                -1000                                  
3                -700
3                 700  
;

data positive negative;
 set have;
 if sign(amount)=-1 then output negative;
  else output positive;
 run;
data negative;
 set negative;
 amount=-amount;
run;
data temp;
 ina=0;inb=0;
 merge positive(in=ina) negative(in=inb);
 by policy_no amount;
 if ina and inb;
run;
data want;
 set temp;
 output;
 amount=-amount;output;
run;


&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Jan 2020 06:12:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-negative-values/m-p/617374#M180904</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-01-15T06:12:04Z</dc:date>
    </item>
    <item>
      <title>Re: Removing negative values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-negative-values/m-p/617410#M180918</link>
      <description>&lt;P&gt;BRO...you are superstar!&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2020 12:05:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-negative-values/m-p/617410#M180918</guid>
      <dc:creator>Solly7</dc:creator>
      <dc:date>2020-01-15T12:05:10Z</dc:date>
    </item>
    <item>
      <title>Re: Removing negative values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-negative-values/m-p/617419#M180924</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
   CREATE TABLE want AS
      SELECT DISTINCT a.*
      FROM have a
      INNER JOIN have b 
      ON a.policy_no eq b.policy_no
      AND ((a.amount&amp;lt;0 AND abs(a.amount) eq b.amount)
           OR (a.amount&amp;gt;0 AND a.amount eq abs(b.amount)))
   ;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Jan 2020 12:49:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-negative-values/m-p/617419#M180924</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2020-01-15T12:49:43Z</dc:date>
    </item>
    <item>
      <title>Re: Removing negative values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Removing-negative-values/m-p/620455#M182317</link>
      <description>&lt;P&gt;&lt;SPAN&gt;HI&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/296078" target="_blank"&gt;@Solly7&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Felt my solution will be more simplified and this worked. you can try this.&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; Data Have        ;                                 
input policy_no    amount    ;
cards; 
1                -500                                     
1                -500                                
1                 500                                                             
2                -1000                                  
3                -700
3                 700 
3                 100 
;
run;

proc sort data=have nodup;
by policy_no    amount;
run;
data want;
set have;
by policy_no    amount;
if first.policy_no ne last.policy_no;
output;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jan 2020 09:44:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Removing-negative-values/m-p/620455#M182317</guid>
      <dc:creator>srujana_hm</dc:creator>
      <dc:date>2020-01-28T09:44:22Z</dc:date>
    </item>
  </channel>
</rss>

