<?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: Comparing row by row and find column do not match in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Comparing-row-by-row-and-find-column-do-not-match/m-p/678542#M204807</link>
    <description>Thanks</description>
    <pubDate>Fri, 21 Aug 2020 18:24:34 GMT</pubDate>
    <dc:creator>mazouz</dc:creator>
    <dc:date>2020-08-21T18:24:34Z</dc:date>
    <item>
      <title>Comparing row by row and find column do not match</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-row-by-row-and-find-column-do-not-match/m-p/678355#M204710</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I want to compare negative line by positive line in terms of price. the comparison is made on the variables (name, city, age, product). I want a table which contains just the negatives with their positive correspondents and the name of the variable which is different between the two rows, if I find more than one difference between two rows I do not put it in this table like the case of " Oliver "I have two differences in the city and the age.&lt;/P&gt;&lt;PRE&gt;data have;&lt;BR /&gt;input  row name $ city $ age product $ price;&lt;BR /&gt;CARDS;&lt;BR /&gt;1 Philip Paris 43 P1 -10&lt;BR /&gt;2 Mikael Marseille 23 P2 -20&lt;BR /&gt;3 Oliver Metz 32 P2 -25&lt;BR /&gt;4 Lisa Toulouse 43 P1 -15&lt;BR /&gt;5 Philip Paris 55 P1 10&lt;BR /&gt;6 Michael Marseille 23 P2 20&lt;BR /&gt;7 Oliver Lille 23 P1 25&lt;BR /&gt;8 Lisa Toulouse 43 P2 15&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;input negative_row name $ city $ age product $ price positive_row column_diff $;&lt;BR /&gt;CARDS;&lt;BR /&gt;1 Philip Paris 43 P1 -10 5 age&lt;BR /&gt;2 Mikael Marseille 23 P2 -20 6 name  &lt;BR /&gt;4 Lisa Toulouse 43 P1 -15 8 product&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Aug 2020 07:26:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-row-by-row-and-find-column-do-not-match/m-p/678355#M204710</guid>
      <dc:creator>mazouz</dc:creator>
      <dc:date>2020-08-21T07:26:50Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing row by row and find column do not match</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-row-by-row-and-find-column-do-not-match/m-p/678374#M204719</link>
      <description>&lt;P&gt;Try next tested code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input  row name $ city $ age product $ price;
CARDS;
1 Philip Paris 43 P1 -10
2 Mikael Marseille 23 P2 -20
3 Oliver Metz 32 P2 -25
4 Lisa Toulouse 43 P1 -15
5 Philip Paris 55 P1 10
6 Michael Marseille 23 P2 20
7 Oliver Lille 23 P1 25
8 Lisa Toulouse 43 P2 15
;
run;

proc sql;
  create table temp as select 
  n.*, 
  p.row as positive_row,
  p.city as p_city,
  p.name as p_name,
  p.product as p_product,
  p.age as p_age,
  (p.price + n.price) as diff
  from have(where=(price &amp;lt; 0)) as n 
  full join have(where=(price&amp;gt;0)) as p
  on n.price = -p.price
  order by row;
quit;
data want;
 set temp(rename=(row=negative_row_));
     length column_diff $6;
     /* d# is either true=1 or false=0 */
     d1 = (name=p_name);
     d2 = (city=p_city);
     d3 = (age=P_age);
     d4 = (product=p_product);
     d5 = (diff=0);
     if sum(of d1-d5) = 4    /* 4 out of 5 variables matches excluding row */
        then do;
           if not d1 then  column_diff = "name"; else
           if not d2 then  column_diff = "city"; else
           if not d3 then  column_diff = "age"; else
           if not d4 then  column_diff = "product"; else
           if not d5 then  column_diff = "price"; 
           output;
        end;
     keep negative_row_ name city age product price positive_row column_diff;
run;
           
           
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Aug 2020 09:56:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-row-by-row-and-find-column-do-not-match/m-p/678374#M204719</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-08-21T09:56:08Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing row by row and find column do not match</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-row-by-row-and-find-column-do-not-match/m-p/678380#M204723</link>
      <description>it's working when I change this: I add not&lt;BR /&gt;if not d1 then column_diff = "name"; else&lt;BR /&gt;if not d2 then column_diff = "city"; else&lt;BR /&gt;if not d3 then column_diff = "age"; else&lt;BR /&gt;if not d4 then column_diff = "product"; else&lt;BR /&gt;if not d5 then column_diff = "price";</description>
      <pubDate>Fri, 21 Aug 2020 09:04:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-row-by-row-and-find-column-do-not-match/m-p/678380#M204723</guid>
      <dc:creator>mazouz</dc:creator>
      <dc:date>2020-08-21T09:04:14Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing row by row and find column do not match</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-row-by-row-and-find-column-do-not-match/m-p/678392#M204732</link>
      <description>&lt;P&gt;You are right, I'll edit the code.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Aug 2020 09:54:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-row-by-row-and-find-column-do-not-match/m-p/678392#M204732</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-08-21T09:54:56Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing row by row and find column do not match</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-row-by-row-and-find-column-do-not-match/m-p/678413#M204744</link>
      <description>&lt;P&gt;last part of the code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;        then do;
           if not d1 then  column_diff = "name"; else
           if not d2 then  column_diff = "city"; else
           if not d3 then  column_diff = "age"; else
           if not d4 then  column_diff = "product"; else
           if not d5 then  column_diff = "price"; 
           output;
        end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;can be shortened and replaced with next code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;     array nx {5} $ ("name" "city" "age" "product" "price");
     if sum(of d1-d5) = 4    /* 4 out of 5 variables matches excluding row */
        then do; &lt;BR /&gt;             column_diff = nx(whichn(0,of d1-d5)); &lt;BR /&gt;             output;&lt;BR /&gt;        end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;thus making it more dynamic to adapt to greater number of variables.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Aug 2020 11:46:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-row-by-row-and-find-column-do-not-match/m-p/678413#M204744</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-08-21T11:46:50Z</dc:date>
    </item>
    <item>
      <title>Re: Comparing row by row and find column do not match</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Comparing-row-by-row-and-find-column-do-not-match/m-p/678542#M204807</link>
      <description>Thanks</description>
      <pubDate>Fri, 21 Aug 2020 18:24:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Comparing-row-by-row-and-find-column-do-not-match/m-p/678542#M204807</guid>
      <dc:creator>mazouz</dc:creator>
      <dc:date>2020-08-21T18:24:34Z</dc:date>
    </item>
  </channel>
</rss>

