<?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: Matching Data in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615483#M76974</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/305622"&gt;@Saslearning2020&lt;/a&gt;&amp;nbsp; Pardon me for the delay.&amp;nbsp; Methinks this slight change&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; if sign(pay)=-1 and h.check(key:abs(pay)) then continue;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;should suffice.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Full version :&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;
   dcl hash H (multidata:'y') ;
   h.definekey  ("pay") ;
   h.definedone () ;
 end;
 do _n_=1 by 1 until(last.id);
  set have;
  by id;
  if sign(pay)=-1 then _iorc_=h.add(key:abs(pay),data:abs(pay));
 end;
 do _n_=1 to _n_;
  set have;
  if sign(pay)=-1 and h.check(key:abs(pay)) then continue;
  if h.check()=0 then do;
    h.removedup();
    continue;
  end;
  output;
 end;
 h.clear();
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Please try and let me know. Thanks!&lt;/P&gt;</description>
    <pubDate>Mon, 06 Jan 2020 21:48:20 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2020-01-06T21:48:20Z</dc:date>
    <item>
      <title>Matching Data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615230#M76944</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I am struggling to merge two databases and wondering whether I can get some advice from this community.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example&lt;/P&gt;&lt;P&gt;Dataset A has three variables:&lt;/P&gt;&lt;P&gt;ID Seqnum Pay&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 7&amp;nbsp; &amp;nbsp;-10&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 12&amp;nbsp;&amp;nbsp; -10&lt;/P&gt;&lt;P&gt;1&amp;nbsp; 13&amp;nbsp;&amp;nbsp; -5&lt;/P&gt;&lt;P&gt;2&amp;nbsp; 16&amp;nbsp;&amp;nbsp; -15&lt;/P&gt;&lt;P&gt;3&amp;nbsp; 20&amp;nbsp;&amp;nbsp; -20&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Dataset B has three variable&lt;/P&gt;&lt;P&gt;ID &amp;nbsp;Seqnum &amp;nbsp;Pay&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp; &amp;nbsp;8&amp;nbsp;&amp;nbsp; 10&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp; &amp;nbsp;9&amp;nbsp;&amp;nbsp; 10&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp; &amp;nbsp;10&amp;nbsp;&amp;nbsp; 10&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp; &amp;nbsp;11&amp;nbsp;&amp;nbsp; 5&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp; &amp;nbsp;14&amp;nbsp;&amp;nbsp; 15&lt;/P&gt;&lt;P&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp; 15&amp;nbsp; &amp;nbsp;15&lt;/P&gt;&lt;P&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp; 17&amp;nbsp; &amp;nbsp;25&lt;/P&gt;&lt;P&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp; 18&amp;nbsp; &amp;nbsp;15&lt;/P&gt;&lt;P&gt;3&amp;nbsp;&amp;nbsp; &amp;nbsp;19&amp;nbsp; 20&lt;/P&gt;&lt;P&gt;3&amp;nbsp;&amp;nbsp; &amp;nbsp;21&amp;nbsp; 25&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All I want is each negative payment in my source file Dataset A should look in Dataset B, match by ID and set off the first matched positive payment. Output should be:&lt;/P&gt;&lt;P&gt;ID &amp;nbsp;Seqnum &amp;nbsp;Pay&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp; &amp;nbsp;10&amp;nbsp;&amp;nbsp; 10&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp; &amp;nbsp;14&amp;nbsp;&amp;nbsp; 15&lt;/P&gt;&lt;P&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp; 17&amp;nbsp; &amp;nbsp;25&lt;/P&gt;&lt;P&gt;2&amp;nbsp;&amp;nbsp;&amp;nbsp; 18&amp;nbsp; &amp;nbsp;15&lt;/P&gt;&lt;P&gt;3&amp;nbsp;&amp;nbsp; &amp;nbsp;21&amp;nbsp; 25&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jan 2020 00:30:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615230#M76944</guid>
      <dc:creator>Saslearning2020</dc:creator>
      <dc:date>2020-01-06T00:30:42Z</dc:date>
    </item>
    <item>
      <title>Re: Matching Data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615231#M76945</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/305622"&gt;@Saslearning2020&lt;/a&gt;&amp;nbsp; Welcome to SAS communities. I am afraid I am unable to comprehend&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;"All I want is each negative payment in my source file Dataset A should look in Dataset B, match by ID and set off the first matched positive payment"&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you please explain and elaborate the logic with reference to your expect output?&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jan 2020 00:00:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615231#M76945</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-06T00:00:37Z</dc:date>
    </item>
    <item>
      <title>Re: Matching Data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615232#M76946</link>
      <description>&lt;P&gt;These are medical records. For example,&amp;nbsp; patient 1 (ID=1) got charged for $5 (seqnum = 11), but then it was found that this charge was wrong and it got cancelled out （i.e. seqnum = 13, pay = -5). I want to get a cleaned data set without the positive records which got cancelled later. These two data sets are originally combined together and I separated them into 2 data sets with one only having negative payments and the other only having positive payments.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jan 2020 00:14:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615232#M76946</guid>
      <dc:creator>Saslearning2020</dc:creator>
      <dc:date>2020-01-06T00:14:35Z</dc:date>
    </item>
    <item>
      <title>Re: Matching Data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615233#M76947</link>
      <description>&lt;P&gt;Looks like transaction data? You're trying to keep 'un-balanced' transactions?&lt;/P&gt;
&lt;P&gt;Take a look at this...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
	input id seqnum pay;
	datalines;
1 12 -10
1 13 -5
2 16 -15
3 20 -20
;
run;

data b;
	input id seqnum pay;
	datalines;
1 8 10
1 9 10
1 10 10
1 11 5
1 14 15
2 15 15
2 17 25
2 18 15
3 19 20
3 21 25
;
run;

data desired_output;
	input id seqnum pay;
	datalines;
1 9 10
1 10 10
1 14 15
2 17 25
2 18 15
3 21 25
;
run;

/* Edit a so that pay=-1*pay */
data a_edited(rename=(pay_neg=pay));
	set a;
	pay_neg=-1*pay;
	drop pay;
run;


/* Sort and merge */
proc sort data=b;
	by id pay;
run;

proc sort data=a_edited;
	by id pay;
run;

data want;
	merge b(in=B) a_edited(in=a rename=(seqnum=seqnum_a));
	by id pay;

	if b;

	if not first.pay or seqnum_a=.;
	drop seqnum_a;
run;

/* Checking that desired_output=want */
proc sort data=want;
	by id seqnum;
run;

proc compare base=desired_output compare=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 06 Jan 2020 00:32:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615233#M76947</guid>
      <dc:creator>unison</dc:creator>
      <dc:date>2020-01-06T00:32:14Z</dc:date>
    </item>
    <item>
      <title>Re: Matching Data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615237#M76950</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/305622"&gt;@Saslearning2020&lt;/a&gt;&amp;nbsp; &amp;nbsp;I don't think you need to split the data. You can keep as one and do bygroup processing.&lt;/P&gt;
&lt;P&gt;Assuming your PAY values are only &lt;EM&gt;INTEGERS&lt;/EM&gt;, it makes a sweet spot for &lt;EM&gt;KEY INDEXING&lt;/EM&gt; paint brush method using ARRAYS. You could consider the following where I have combined A and B into one dataset and named that as HAVE&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
	input id seqnum pay;
	datalines;
1 12 -10
1 13 -5
2 16 -15
3 20 -20
;
run;

data b;
	input id seqnum pay;
	datalines;
1 8 10
1 9 10
1 10 10
1 11 5
1 14 15
2 15 15
2 17 25
2 18 15
3 19 20
3 21 25
;
run;

data have;
set a b;
by 	id seqnum;
run;

data want;
 do _n_=1 by 1 until(last.id);
  set have;
  by id;
  array t(99999) _temporary_;
  if sign(pay)=-1 then t(abs(pay))=1;
 end;
 do _n_=1 to _n_;
  set have;
  if sign(pay)=-1 then continue;
  if t(pay) then do;
   call missing(t(pay));
   continue;
  end;
  output;
 end;
 call missing(of t(*));
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;/*Or*/&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;
   dcl hash H () ;
   h.definekey  ("pay") ;
   h.definedone () ;
 end;
 do _n_=1 by 1 until(last.id);
  set have;
  by id;
  if sign(pay)=-1 then _iorc_=h.add(key:abs(pay),data:abs(pay));
 end;
 do _n_=1 to _n_;
  set have;
  if sign(pay)=-1 then continue;
  if h.check()=0 then do;
    h.remove();
    continue;
  end;
  output;
 end;
 h.clear();
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/270457"&gt;@unison&lt;/a&gt;&amp;nbsp; &amp;nbsp;You win my friend. I understood the question on seeing your solution. I admire your great analytical skills not just on this thread but on many others. I am posting this solution for my fun. Yours is original. Cheers!&lt;EM&gt; Tonight was Heineke&lt;/EM&gt;n &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;</description>
      <pubDate>Mon, 06 Jan 2020 03:52:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615237#M76950</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-06T03:52:22Z</dc:date>
    </item>
    <item>
      <title>Re: Matching Data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615245#M76952</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am understanding your problem as you are having a data set that contains a list of transactions that have been paid, and another data set that contains a list of transactions that reversed charges in the first list. Your objective is to purge all the transactions that have been reversed and keep only charges that were paid but never reversed. If I understood your problem correctly, then the following should address it.&amp;nbsp; I have included verbose comments to explain the logic, and have used the data set you have provided to test the code. You should be able to cut and paste the code into sas and run it, and look at the log, and the output to see if it is producing what you want.&lt;/P&gt;&lt;P&gt;I hope this helps.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;&lt;P&gt;** Select everything below this line, and paste it in sas and run it;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data Reverse_transactions;&lt;BR /&gt;input id seqnum pay;&lt;BR /&gt;datalines;&lt;BR /&gt;1 7 -10&lt;BR /&gt;1 12 -10&lt;BR /&gt;1 13 -5&lt;BR /&gt;2 16 -15&lt;BR /&gt;3 20 -20&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;data Charges;&lt;BR /&gt;input id seqnum pay;&lt;BR /&gt;datalines;&lt;BR /&gt;1 8 10&lt;BR /&gt;1 9 10&lt;BR /&gt;1 10 10&lt;BR /&gt;1 11 5&lt;BR /&gt;1 14 15&lt;BR /&gt;2 15 15&lt;BR /&gt;2 17 25&lt;BR /&gt;2 18 15&lt;BR /&gt;3 19 20&lt;BR /&gt;3 21 25&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;/* It is more useful to have the reverse transaction&lt;BR /&gt;with pay being positive, it will make the matching more straightforwad.*/&lt;BR /&gt;/* I am also going to rename seqnum to reverse_seqnum, this is not needed&lt;BR /&gt;but will be helpful for debugging; later it will show which transactions were cancelled&lt;BR /&gt;by which transactions*/&lt;BR /&gt;data pos_reverse_trans (rename=(seqnum=Reverse_seqnum));&lt;BR /&gt;* remove the rename once you finish&lt;/P&gt;&lt;P&gt;debugging or drop the column in you final data set;&lt;BR /&gt;set reverse_transactions;&lt;BR /&gt;pay=-pay;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;/* now the idea is to consider the pos_reverse_trans like a look up table&lt;BR /&gt;we will consider each transaction in the charges data set, if we find a matching&lt;BR /&gt;transaction in the pos_reverse_trans, then we considere that transaction reveresed&lt;BR /&gt;and we do not keep it. Basically the problem is essentially do a match merge of&lt;BR /&gt;the charges and the pos_reverse_trans and keep only the oservations that are in the&lt;BR /&gt;charges data set but not in the pos_reverse_transaction set.*/&lt;BR /&gt;* Need to prepare the data set for merge by sorting on by ID and Pay;&lt;/P&gt;&lt;P&gt;/* I am sorting the data sets in place here, instead of making a sorted copy.&lt;BR /&gt;Please do that if you want to preserve the original data set*/&lt;BR /&gt;proc sort data=charges;&lt;BR /&gt;by ID pay;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc sort data=pos_reverse_trans;&lt;BR /&gt;by ID pay;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data non_reversed_charges;&lt;BR /&gt;merge charges(in=C) pos_reverse_trans(in=R);&lt;BR /&gt;putlog _ALL_;&lt;/P&gt;&lt;P&gt;/* take a look at the log to see how the matching was done, remove once you know&lt;BR /&gt;it is working as desired and drop reverse_seqnum column; */&lt;BR /&gt;if R=0;&lt;BR /&gt;* if the transaction was not reversed implicit write it to the non_reversed_charges;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jan 2020 03:40:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615245#M76952</guid>
      <dc:creator>jhammouda</dc:creator>
      <dc:date>2020-01-06T03:40:53Z</dc:date>
    </item>
    <item>
      <title>Re: Matching Data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615260#M76953</link>
      <description>&lt;P&gt;Hi Novinosrin,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think there are few issues with both the array and hash map solutions provided. You can test the code with the following data set (a), which essentially states that every transaction has been reversed, i.e&amp;nbsp; The want set that you define should have zero observations. However, both the array and the hash map code produce a set that has 3 elements. The code trips on reverse transactions of the same amount for the same ID. Your code assumes that an amount of reversal (negative amount) is unique per ID. Having said that I do not think it would be reasonable to assume that the payments are integers and that they could be used as indices. I did not dig deeper into your code, but you are grouping by ID and Seqnum. Though you have not done anything with seqnum, one should not have any logic depending on seqnum given the information provided. The only assumption that could be made about seqnum is that a transaction that reverses a prior one, has a higher sequence number than the one it reversed.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think there is merit to splitting the data as it leads to a straight forward merge (simple code, less error-prone) which is fairly efficient and would be more efficient than using one array that has all the data. Even if you prefer to solve this problem using an array you would want to have 2 arrays to take advantage of the fact that the number of reversals is typically much smaller than the number of all the paid transactions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best regards,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data a ;&lt;BR /&gt;input id seqnum pay;&lt;BR /&gt;datalines;&lt;BR /&gt;1 12 -10&lt;BR /&gt;1 13 -10&lt;BR /&gt;1 16 -10&lt;BR /&gt;1 30 -5&lt;BR /&gt;1 22 -15&lt;BR /&gt;2 24 -15&lt;BR /&gt;2 20 -25&lt;BR /&gt;2 23 -15&lt;BR /&gt;3 28 -20&lt;BR /&gt;3 27 -25&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jan 2020 06:56:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615260#M76953</guid>
      <dc:creator>jhammouda</dc:creator>
      <dc:date>2020-01-06T06:56:39Z</dc:date>
    </item>
    <item>
      <title>Re: Matching Data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615317#M76959</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/301697"&gt;@jhammouda&lt;/a&gt;&amp;nbsp; First off, Thank you for taking the time to write and explain at length what the requirement is. I really do like how you portrayed the paid and reversal of transactions. Second of all, your professional demeanor is very noteworthy and I would like to copy such traits in my posts while so far all my participation has been entertainment playing a video game called SAS.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Feeling obliged, I took your suggested sample A. Basically, the statement or grouping&lt;STRONG&gt;&lt;EM&gt; BY ID SEQNUM&lt;/EM&gt;&amp;nbsp;&lt;/STRONG&gt;that you mentioned I am using in the logic isn't quite what it is. My apologies for not commenting nor explaining.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The original dataset before the split(&lt;EM&gt;which I still think is not needed&lt;/EM&gt;) should be a combined A+B in the order of SEQNUM, that's all.&amp;nbsp; Of course then +ve goes to B and -ve goes to A. Fair enough. Yes, the assumption of INTEGERS for ARRAY processing is illsuited though it's not hard to go little brute-force to achieve that for any number values of PAY. Nevertheless, HASH requires a very minor change i.e&amp;nbsp; the inclusion of&amp;nbsp;dcl hash H (multidata:'y') ; and&amp;nbsp; h.removedup(); to address that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, Creating a new sample HAVE combining the suggested A, the full version is modified with the above 2 mentioned HASH changes&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data a ;
input id seqnum pay;
datalines;
1 12 -10
1 13 -10
1 16 -10
1 30 -5
1 22 -15
2 24 -15
2 20 -25
2 23 -15
3 28 -20
3 27 -25
;
run;
data b;
	input id seqnum pay;
	datalines;
1 8 10
1 9 10
1 10 10
1 11 5
1 14 15
2 15 15
2 17 25
2 18 15
3 19 20
3 21 25
;
run;

proc sort data=a;
by id seqnum;
run;

/*Creating the original dataset COMBINE*/
data have;
set a b;
by 	id seqnum;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;/*HASH modified*/&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want ;
 if _n_=1 then do;
   dcl hash H (multidata:'y') ;
   h.definekey  ("pay") ;
   h.definedone () ;
 end;
 do _n_=1 by 1 until(last.id);
  set have;
  by id;
  if sign(pay)=-1 then _iorc_=h.add(key:abs(pay),data:abs(pay));
 end;
 do _n_=1 to _n_;
  set have;
  if sign(pay)=-1 then continue;
  if h.check()=0 then do;
    h.removedup();
    continue;
  end;
  output;
 end;
 h.clear();
run;&lt;/CODE&gt;&lt;/PRE&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jan 2020 12:55:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615317#M76959</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-06T12:55:57Z</dc:date>
    </item>
    <item>
      <title>Re: Matching Data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615354#M76963</link>
      <description>&lt;P&gt;That would work.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jan 2020 14:31:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615354#M76963</guid>
      <dc:creator>jhammouda</dc:creator>
      <dc:date>2020-01-06T14:31:00Z</dc:date>
    </item>
    <item>
      <title>Re: Matching Data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615390#M76966</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;Thank you so much for the codes. It does the trick. After running the code on my original data, there came another issue. Due to entry errors, there are some negative payments which cannot be matched with positive payments. In such case, the current codes will not report these unmatched negative payments in the output. Is there a way to still show these unmatched negative &lt;FONT color="#000000"&gt;payments? For example, if a dataline " &lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;1 230 -1500&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;" is added to&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&amp;nbsp;data a&amp;nbsp; and&amp;nbsp;a dataline "&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;1 231 1400" is added to data b,&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;the current output is&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token keyword"&gt;id&lt;/SPAN&gt; seqnum pay&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;1 231 1400&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;CODE class="  language-sas"&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;Is there a way to show the output as&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token keyword"&gt;id&lt;/SPAN&gt; seqnum pay&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;1 230 -1500&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;1 231 1400&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jan 2020 17:33:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615390#M76966</guid>
      <dc:creator>Saslearning2020</dc:creator>
      <dc:date>2020-01-06T17:33:53Z</dc:date>
    </item>
    <item>
      <title>Re: Matching Data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615396#M76967</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp; Thank you very much for the codes. It works for my data. After running it on my original data, there came one issue. Due to entry errors by billing staff, some negative payments cannot be matched. In this case, the current codes will not report the unmatched negative payments. Is there a way to still show them?&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;For example, if a dataline "&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;1 230 -1500&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt; " is added to data a and a dataline "&amp;nbsp;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;1 231 1400" is added to data b, then the current output is:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;CODE class="  language-sas"&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;ID seqnum pay&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;1 231 1400&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;CODE class="  language-sas"&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;Is it possible to show:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;CODE class="  language-sas"&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;ID seqnum pay&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;CODE class="  language-sas"&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;1 230 -1500&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;1 231 1400&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;CODE class="  language-sas"&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;CODE class="  language-sas"&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token data string"&gt;Thanks.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/P&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>Mon, 06 Jan 2020 17:45:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615396#M76967</guid>
      <dc:creator>Saslearning2020</dc:creator>
      <dc:date>2020-01-06T17:45:11Z</dc:date>
    </item>
    <item>
      <title>Re: Matching Data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615400#M76969</link>
      <description>&lt;P&gt;Hey&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/305622"&gt;@Saslearning2020&lt;/a&gt;&amp;nbsp; &amp;nbsp;Can you plz hang in there if you need my help. My boss and a few others are sitting besides me. I can look into it End of day.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It irks me they turned up to office but they usually work from home.&amp;nbsp;&lt;img id="smileysad" class="emoticon emoticon-smileysad" src="https://communities.sas.com/i/smilies/16x16_smiley-sad.png" alt="Smiley Sad" title="Smiley Sad" /&gt; I will try asap&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jan 2020 17:49:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615400#M76969</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-06T17:49:53Z</dc:date>
    </item>
    <item>
      <title>Re: Matching Data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615406#M76970</link>
      <description>Of course whenever it's convenient for you. Thanks so much!</description>
      <pubDate>Mon, 06 Jan 2020 17:59:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615406#M76970</guid>
      <dc:creator>Saslearning2020</dc:creator>
      <dc:date>2020-01-06T17:59:07Z</dc:date>
    </item>
    <item>
      <title>Re: Matching Data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615420#M76972</link>
      <description>&lt;P&gt;Hi Saslearning2020,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Try this, it should do what you wanted. By the may my original posting had a missing by ID pay statement in thee merge data step.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Good luck&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
	input id seqnum pay;
	
	datalines;
1 12 -10
1 13 -10
1 16 -10	
1 30 -5
1 22 -15
1 230 -1500
2 24 -15
2 20 -25
2 23 -15
3 28 -20
3 27 -25
;
run;


data b;
	input id seqnum pay;
	datalines;
1 8 10
1 9 10
1 10 10
1 11 5
1 14 15
1 231 1400
2 15 15
2 17 25
2 18 15
3 19 20
3 21 25
;
run;

data pos_a ;
	set a;
	pay=-pay;
run;

proc sort data=b;
	by ID pay;
run;

proc sort data=pos_a;
	by ID pay;
run;

data non_matched;
	merge b(in=C) pos_a(in=R);
	by id pay;
	if R=0 then output;* this is in b but not in pos_a;
	else if C=0 then do;*this is in pos_a but not in b;
	pay=-pay;*convert it back to to its original negtive form;
	output;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jan 2020 18:50:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615420#M76972</guid>
      <dc:creator>jhammouda</dc:creator>
      <dc:date>2020-01-06T18:50:57Z</dc:date>
    </item>
    <item>
      <title>Re: Matching Data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615421#M76973</link>
      <description>&lt;P&gt;need to add by ID pay; right after the merge statement;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jan 2020 18:52:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615421#M76973</guid>
      <dc:creator>jhammouda</dc:creator>
      <dc:date>2020-01-06T18:52:42Z</dc:date>
    </item>
    <item>
      <title>Re: Matching Data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615483#M76974</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/305622"&gt;@Saslearning2020&lt;/a&gt;&amp;nbsp; Pardon me for the delay.&amp;nbsp; Methinks this slight change&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; if sign(pay)=-1 and h.check(key:abs(pay)) then continue;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;should suffice.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Full version :&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;
   dcl hash H (multidata:'y') ;
   h.definekey  ("pay") ;
   h.definedone () ;
 end;
 do _n_=1 by 1 until(last.id);
  set have;
  by id;
  if sign(pay)=-1 then _iorc_=h.add(key:abs(pay),data:abs(pay));
 end;
 do _n_=1 to _n_;
  set have;
  if sign(pay)=-1 and h.check(key:abs(pay)) then continue;
  if h.check()=0 then do;
    h.removedup();
    continue;
  end;
  output;
 end;
 h.clear();
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Please try and let me know. Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 06 Jan 2020 21:48:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615483#M76974</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-06T21:48:20Z</dc:date>
    </item>
    <item>
      <title>Re: Matching Data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615846#M77000</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp; Thanks. It works. Really appreciated.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jan 2020 03:36:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Matching-Data/m-p/615846#M77000</guid>
      <dc:creator>Saslearning2020</dc:creator>
      <dc:date>2020-01-08T03:36:33Z</dc:date>
    </item>
  </channel>
</rss>

