<?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 Flagging column differences among rows with the same ID in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Flagging-column-differences-among-rows-with-the-same-ID/m-p/880571#M347922</link>
    <description>&lt;P&gt;Hi there,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a very large dataset with 40 variables, where an ID can repeat more than once. When an ID appears more than once, is there a way I can flag differences in variable values between the rows?&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Fruit&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Color&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Vehicle&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Year&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Apple&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Blue&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Car&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2001&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Apple&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Purple&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Car&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2001&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Banana&amp;nbsp; &amp;nbsp; Red&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Van&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2000&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Banana&amp;nbsp; &amp;nbsp; Blue&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Car&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2000&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the above example, I would like to flag that color differs for ID 1 , and that color and&amp;nbsp; vehicle differs for ID 2.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for any suggestions that you might be able to offer!&lt;/P&gt;</description>
    <pubDate>Wed, 14 Jun 2023 02:54:07 GMT</pubDate>
    <dc:creator>jnivi</dc:creator>
    <dc:date>2023-06-14T02:54:07Z</dc:date>
    <item>
      <title>Flagging column differences among rows with the same ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flagging-column-differences-among-rows-with-the-same-ID/m-p/880571#M347922</link>
      <description>&lt;P&gt;Hi there,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a very large dataset with 40 variables, where an ID can repeat more than once. When an ID appears more than once, is there a way I can flag differences in variable values between the rows?&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Fruit&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Color&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Vehicle&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Year&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Apple&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Blue&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Car&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2001&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Apple&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Purple&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Car&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2001&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Banana&amp;nbsp; &amp;nbsp; Red&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Van&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2000&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Banana&amp;nbsp; &amp;nbsp; Blue&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Car&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2000&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the above example, I would like to flag that color differs for ID 1 , and that color and&amp;nbsp; vehicle differs for ID 2.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for any suggestions that you might be able to offer!&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2023 02:54:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flagging-column-differences-among-rows-with-the-same-ID/m-p/880571#M347922</guid>
      <dc:creator>jnivi</dc:creator>
      <dc:date>2023-06-14T02:54:07Z</dc:date>
    </item>
    <item>
      <title>Re: Flagging column differences among rows with the same ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flagging-column-differences-among-rows-with-the-same-ID/m-p/880611#M347945</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/439729"&gt;@jnivi&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How about creating a dataset containing the ID and a character variable DIFF: a string of flags which are "1" if the corresponding variable in your dataset has two or more different values for that ID and "0" otherwise? So the result for your sample data would look like this:&lt;/P&gt;
&lt;PRE&gt;ID    diff

 1    0100
 2    0110
&lt;/PRE&gt;
&lt;P&gt;For ID 1 only the second variable (after ID), i.e. &lt;FONT face="courier new,courier"&gt;Color&lt;/FONT&gt;, exhibits a difference.&amp;nbsp;For ID 2 it's also the third, &lt;FONT face="courier new,courier"&gt;Vehicle&lt;/FONT&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Fruit $ Color $ Vehicle $ Year;
cards;
1 Apple Blue Car 2001
1 Apple Purple Car 2001
2 Banana Red Van 2000
2 Banana Blue Car 2000
;

filename compvals temp;

data _null_;
file compvals;
set sashelp.vcolumn end=last;
where libname='WORK' &amp;amp; memname='HAVE' &amp;amp; name ne 'ID';
pos=varnum-1;
put 'if ' name 'ne lag(' name +(-1) ') then substr(diff,' pos +(-1) ',1)="1";';
if last then call symputx('nvars',_n_);
run;

data want(keep=id diff);
do until(last.id);
  set have;
  by id;
  length diff $&amp;amp;nvars;
  %inc compvals;
  if first.id then diff=repeat('0',&amp;amp;nvars-1);
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you are using the VALIDVARNAME=ANY system option and non-standard variable names (e.g., names containing blanks), the code above needs to be tweaked a bit (NLITERAL function).&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2023 11:02:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flagging-column-differences-among-rows-with-the-same-ID/m-p/880611#M347945</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2023-06-14T11:02:18Z</dc:date>
    </item>
    <item>
      <title>Re: Flagging column differences among rows with the same ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flagging-column-differences-among-rows-with-the-same-ID/m-p/880623#M347950</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID       ( Fruit          Color           Vehicle  ) ($)          Year;
cards;
1          Apple        Blue              Car                   2001
1          Apple       Purple           Car                   2001
2         Banana    Red                Van                  2000
2        Banana    Blue               Car                    2000
;

proc transpose data=have(obs=0) out=temp;
var _all_;
run;
proc sql noprint;
select cat('count(distinct ',_name_,') as ',_name_) into :count separated by ' ,' from temp;

create table temp2 as
select id as _id,&amp;amp;count. from have group by id;
quit;
proc transpose data=temp2 out=want(where=(col1 ne 1));
by _id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1686743996778.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/84964iD1B8E74783DF9F53/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1686743996778.png" alt="Ksharp_0-1686743996778.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 14 Jun 2023 11:59:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flagging-column-differences-among-rows-with-the-same-ID/m-p/880623#M347950</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-06-14T11:59:49Z</dc:date>
    </item>
    <item>
      <title>Re: Flagging column differences among rows with the same ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flagging-column-differences-among-rows-with-the-same-ID/m-p/881747#M348418</link>
      <description>&lt;P&gt;Thank you!! This was very helpful and worked! Some of the differences between the records with same ID was actually due to missingness. Is there a way that this code can be modified so that "NA" values don't contribute to the difference in variable values amongst records with the same ID?&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jun 2023 16:39:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flagging-column-differences-among-rows-with-the-same-ID/m-p/881747#M348418</guid>
      <dc:creator>jnivi</dc:creator>
      <dc:date>2023-06-21T16:39:13Z</dc:date>
    </item>
    <item>
      <title>Re: Flagging column differences among rows with the same ID</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flagging-column-differences-among-rows-with-the-same-ID/m-p/881888#M348457</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*
You could set "NA" be missing before counting it.
*/
data have;
input ID       ( Fruit          Color           Vehicle  ) ($)          Year;
cards;
1          Apple        Blue              Car                   2001
1          Apple       NA           Car                   2001
2         Banana    Red                Van                  2000
2        Banana    Blue               Car                    2000
;

data have;
 set have;
 array x{*} $ _character_;
do i=1 to dim(x);
 if x{i}="NA" then call missing(x{i});
end;
drop i;
run;

proc transpose data=have(obs=0) out=temp;
var _all_;
run;
proc sql noprint;
select cat('count(distinct ',_name_,') as ',_name_) into :count separated by ' ,' from temp;

create table temp2 as
select id as _id,&amp;amp;count. from have group by id;
quit;
proc transpose data=temp2 out=want(where=(col1 ne 1));
by _id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 22 Jun 2023 12:36:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flagging-column-differences-among-rows-with-the-same-ID/m-p/881888#M348457</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-06-22T12:36:58Z</dc:date>
    </item>
  </channel>
</rss>

