<?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: Swap and combination in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Swap-and-combination/m-p/457398#M115963</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
Input account_id Credit_amount debit_amount;
Cards;
1 10 100
2 20 200
1 100 10
3 20 200
4 10 100
2 200 20
5 10 50
;
Run;

data temp;
   set have;
   n=_N_;
run;

proc sort data=temp;
   by account_id;
run;

data temp;
   set temp;
   flag=0;
   if Credit_amount=lag1(debit_amount) and debit_amount=lag1(Credit_amount) and account_id=lag1(account_id) then flag=1;
run;

proc sort data=temp out=want(drop=n);
   by n;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 25 Apr 2018 16:54:53 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2018-04-25T16:54:53Z</dc:date>
    <item>
      <title>Swap and combination</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Swap-and-combination/m-p/457394#M115961</link>
      <description>Hi all,&lt;BR /&gt;I have below data. I need to flag observations based on combination. Below is the sample data&lt;BR /&gt;&lt;BR /&gt;Data have;&lt;BR /&gt;Input account_id 8. Credit_amount 8. debit_amount 8.;&lt;BR /&gt;Cards;&lt;BR /&gt;1 10 100&lt;BR /&gt;2 20 200&lt;BR /&gt;1 100 10&lt;BR /&gt;3 20 200&lt;BR /&gt;4 10 100&lt;BR /&gt;2 200 20&lt;BR /&gt;5 10 50&lt;BR /&gt;;&lt;BR /&gt;Run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;So if you look at observation 1 and 3&lt;BR /&gt;Debit and credit are interchanged and hence this transaction needs to be flagged.&lt;BR /&gt;&lt;BR /&gt;Same is the case with observations 2 and 7 hence this also needs to be flagged.&lt;BR /&gt;&lt;BR /&gt;So I want output as below&lt;BR /&gt;&lt;BR /&gt;ACCOUNT_ID CREDIT_AMOUNT DEBIT_AMOUNT FLAG&lt;BR /&gt;1 10 100 Y&lt;BR /&gt;2 20 200 Y&lt;BR /&gt;1 100 10 Y&lt;BR /&gt;3 20 200 N&lt;BR /&gt;4 10 100 N&lt;BR /&gt;2 200 20 Y&lt;BR /&gt;5 10 50 N&lt;BR /&gt;&lt;BR /&gt;I tried using concatenation, however look back is something which is I am not able to figure out.&lt;BR /&gt;Any help is really appreciated.&lt;BR /&gt;&lt;BR /&gt;Thanks in advance</description>
      <pubDate>Wed, 25 Apr 2018 16:36:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Swap-and-combination/m-p/457394#M115961</guid>
      <dc:creator>yashpande</dc:creator>
      <dc:date>2018-04-25T16:36:53Z</dc:date>
    </item>
    <item>
      <title>Re: Swap and combination</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Swap-and-combination/m-p/457398#M115963</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
Input account_id Credit_amount debit_amount;
Cards;
1 10 100
2 20 200
1 100 10
3 20 200
4 10 100
2 200 20
5 10 50
;
Run;

data temp;
   set have;
   n=_N_;
run;

proc sort data=temp;
   by account_id;
run;

data temp;
   set temp;
   flag=0;
   if Credit_amount=lag1(debit_amount) and debit_amount=lag1(Credit_amount) and account_id=lag1(account_id) then flag=1;
run;

proc sort data=temp out=want(drop=n);
   by n;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Apr 2018 16:54:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Swap-and-combination/m-p/457398#M115963</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-04-25T16:54:53Z</dc:date>
    </item>
    <item>
      <title>Re: Swap and combination</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Swap-and-combination/m-p/457407#M115966</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/23883"&gt;@yashpande&lt;/a&gt; wrote:&lt;BR /&gt;Hi all,&lt;BR /&gt;I have below data. I need to flag observations based on combination. Below is the sample data&lt;BR /&gt;&lt;BR /&gt;Data have;&lt;BR /&gt;Input account_id 8. Credit_amount 8. debit_amount 8.;&lt;BR /&gt;Cards;&lt;BR /&gt;1 10 100&lt;BR /&gt;2 20 200&lt;BR /&gt;1 100 10&lt;BR /&gt;3 20 200&lt;BR /&gt;4 10 100&lt;BR /&gt;2 200 20&lt;BR /&gt;5 10 50&lt;BR /&gt;;&lt;BR /&gt;Run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;So if you look at observation 1 and 3&lt;BR /&gt;Debit and credit are interchanged and hence this transaction needs to be flagged.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;What is the actual rule involved? How does the data tell us that the values were interchanged? Can your data &lt;STRONG&gt;never &lt;/STRONG&gt;have the credit amount greater than debit amount?&lt;/P&gt;
&lt;P&gt;Suppose you have a slightly different set where you have the first record repeat. What do you want?&lt;/P&gt;
&lt;P&gt;Data have;&lt;BR /&gt;Input account_id 8. Credit_amount 8. debit_amount 8.;&lt;BR /&gt;Cards;&lt;BR /&gt;1 10 100&lt;BR /&gt;2 20 200&lt;BR /&gt;1 100 10&lt;BR /&gt;3 20 200&lt;BR /&gt;4 10 100&lt;BR /&gt;2 200 20&lt;BR /&gt;5 10 50&lt;/P&gt;
&lt;P&gt;1 10 100&lt;BR /&gt;;&lt;BR /&gt;Run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a suspicion there might be date or time component to this problem that has not been mentioned.&lt;/P&gt;</description>
      <pubDate>Wed, 25 Apr 2018 17:19:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Swap-and-combination/m-p/457407#M115966</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-04-25T17:19:57Z</dc:date>
    </item>
    <item>
      <title>Re: Swap and combination</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Swap-and-combination/m-p/457409#M115968</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
Input account_id  Credit_amount  debit_amount ;
Cards;
1 10 100
2 20 200
1 100 10
3 20 200
4 10 100
2 200 20
5 10 50
;
Run;


data _null_;
if 0 then set have;
length flag $8;
if _n_=1 then do;
   dcl hash H (multidata:'y') ;
   h.definekey  ('account_id','Credit_amount','debit_amount') ;
   h.definedata ('account_id','Credit_amount','debit_amount','flag') ;
   h.definedone () ;
end;
set have end=last;
flag='N';
if h.check(key:account_id,key:debit_amount,key:Credit_amount)=0 then do;
flag='Y';
h.replace(key:account_id, key:debit_amount, key:Credit_amount,data:account_id, data:debit_amount, data:Credit_amount ,data:flag);
end;
h.add();
if last then h.output(dataset:'want');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 25 Apr 2018 17:23:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Swap-and-combination/m-p/457409#M115968</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-04-25T17:23:50Z</dc:date>
    </item>
    <item>
      <title>Re: Swap and combination</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Swap-and-combination/m-p/457412#M115970</link>
      <description>&lt;P&gt;An easy way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;smaller = min(credit_amount, debit_amount);&lt;/P&gt;
&lt;P&gt;larger = max(credit_amount, debit_amount);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=test;&lt;/P&gt;
&lt;P&gt;by account_id smaller larger;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set test;&lt;/P&gt;
&lt;P&gt;by account_id smaller larger;&lt;/P&gt;
&lt;P&gt;if larger=smaller then flag='N';&lt;/P&gt;
&lt;P&gt;else if first.larger=0 or last.larger=0 then flag='Y';&lt;/P&gt;
&lt;P&gt;else flag='N';&lt;/P&gt;
&lt;P&gt;drop smaller larger;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Apr 2018 17:29:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Swap-and-combination/m-p/457412#M115970</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-04-25T17:29:35Z</dc:date>
    </item>
  </channel>
</rss>

