<?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: How do I compare equality of values within a variable by subject in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-compare-equality-of-values-within-a-variable-by-subject/m-p/493806#M129994</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id test $ pass $ level $ ;
datalines;
1 a 'Yes' 'hard'
1 b 'Yes' 'hard'
2 a 'Yes' 'hard'
2 b 'Yes' 'medium'
3 a 'Yes' 'medium'
3 b 'No' 'medium'
4 a 'Yes' 'hard'
4 b 'Yes' 'hard'
5 a 'No' 'medium'
5 b 'Yes' 'medium'
;
run;

proc sql;
create table want as
select Id, case when count(distinct pass)&amp;gt;1 or count(distinct level)&amp;gt;1 then 'NO' else 'YES' end as Match
from have
group by id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or the same with ifc&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select Id, ifc(count(distinct pass)&amp;gt;1 or count(distinct level)&amp;gt;1 ,'NO' ,'YES')  as Match
from have
group by id;
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>Sat, 08 Sep 2018 18:07:58 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-09-08T18:07:58Z</dc:date>
    <item>
      <title>How do I compare equality of values within a variable by subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-compare-equality-of-values-within-a-variable-by-subject/m-p/493797#M129988</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to compare the equality of multiple variables in my dataset for each subject. Below is a sample of my data. I need to compare whether the variable PASS is equal for TEST a &amp;amp; b for each subject, then whether the variable LEVEL is equal for that subject for TEST a &amp;amp; b. Then I need to compare whether both variables pass the test for equality. If only one fails, then the variable MATCH (in my want dataset) is set to 'No'.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Dataset&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;data have;&lt;/DIV&gt;&lt;DIV&gt;input id test $ pass $ level $ ;&lt;/DIV&gt;&lt;DIV&gt;datalines;&lt;/DIV&gt;&lt;DIV&gt;1 a 'Yes' 'hard'&lt;/DIV&gt;&lt;DIV&gt;1 b 'Yes' 'hard'&lt;/DIV&gt;&lt;DIV&gt;2 a 'Yes' 'hard'&lt;/DIV&gt;&lt;DIV&gt;2 b 'Yes' 'medium'&lt;/DIV&gt;&lt;DIV&gt;3 a 'Yes' 'medium'&lt;/DIV&gt;&lt;DIV&gt;3 b 'No' 'medium'&lt;/DIV&gt;&lt;DIV&gt;4 a 'Yes' 'hard'&lt;/DIV&gt;&lt;DIV&gt;4 b 'Yes' 'hard'&lt;/DIV&gt;&lt;DIV&gt;5 a 'No' 'medium'&lt;/DIV&gt;&lt;DIV&gt;5 b 'Yes' 'medium'&lt;/DIV&gt;&lt;DIV&gt;;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;data want;&lt;/DIV&gt;&lt;DIV&gt;input id Match $;&lt;/DIV&gt;&lt;DIV&gt;datalines;&lt;/DIV&gt;&lt;DIV&gt;1 'Yes'&lt;/DIV&gt;&lt;DIV&gt;2 'No'&lt;/DIV&gt;&lt;DIV&gt;3 'No'&lt;/DIV&gt;&lt;DIV&gt;4 'Yes'&lt;/DIV&gt;&lt;DIV&gt;5 'No'&lt;/DIV&gt;&lt;DIV&gt;;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I have managed to accomplish this with several proc transpose statements, but I think there must be a simpler / more elegant method to get to the same solution. As a relatively inexperienced programmer, I'd like to improve my code and it's hard to do without knowing how I could be writing code better. Here are the steps I used to get my 'want' dataset:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;proc sort data=have;&lt;/DIV&gt;&lt;DIV&gt;by id;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;proc transpose data=have out=trans;&lt;/DIV&gt;&lt;DIV&gt;by id;&lt;/DIV&gt;&lt;DIV&gt;var pass hard;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;data equal;&lt;/DIV&gt;&lt;DIV&gt;set trans;&lt;/DIV&gt;&lt;DIV&gt;length match $3;&lt;/DIV&gt;&lt;DIV&gt;if col1=col2 then match = "Yes"; else match = "No";&lt;/DIV&gt;&lt;DIV&gt;drop col1 col2 _:;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;proc transpose data=equal out=trans2;&lt;/DIV&gt;&lt;DIV&gt;by id;&lt;/DIV&gt;&lt;DIV&gt;var match;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;data want;&lt;/DIV&gt;&lt;DIV&gt;set trans2;&lt;/DIV&gt;&lt;DIV&gt;length match $3;&lt;/DIV&gt;&lt;DIV&gt;if col1 = 'No' OR col2 = 'No' then match= 'No'; else match = 'Yes';&lt;/DIV&gt;&lt;DIV&gt;drop col1 col2 _:;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class="yj6qo"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="adL"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Sat, 08 Sep 2018 17:18:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-compare-equality-of-values-within-a-variable-by-subject/m-p/493797#M129988</guid>
      <dc:creator>gadnuk</dc:creator>
      <dc:date>2018-09-08T17:18:14Z</dc:date>
    </item>
    <item>
      <title>Re: How do I compare equality of values within a variable by subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-compare-equality-of-values-within-a-variable-by-subject/m-p/493801#M129990</link>
      <description>&lt;P&gt;The LAG function is your friend:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id test $ pass $ level $ ;
datalines;
1 a 'Yes' 'hard'
1 b 'Yes' 'hard'
2 a 'Yes' 'hard'
2 b 'Yes' 'medium'
3 a 'Yes' 'medium'
3 b 'No' 'medium'
4 a 'Yes' 'hard'
4 b 'Yes' 'hard'
5 a 'No' 'medium'
5 b 'Yes' 'medium'
;
run;
data want;
  set have;
  by id;
  if pass=lag(pass) and level=lag(level) then match='YES';
  else match='NO';
  if last.id;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The "by id" statement creates the automatic variables first.id and last.id, indicating whether the record-in-hand is the first or last record for a given id.&lt;/LI&gt;
&lt;LI&gt;For every incoming record update the queue of "historic" pass values, same with level.&amp;nbsp; The queue is only one member deep (lag2 would have two members).&amp;nbsp; So the if test effectively compares current and preceding value, and assign MATCH based on the result.&lt;/LI&gt;
&lt;LI&gt;The "if last.id" is a subsetting if, resulting in output only for the last record of each id.&amp;nbsp; So as long as you have 2 records per id, you are not contaminating the result data set with comparison of the beginning of one id vs the end of the prior id.&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Sat, 08 Sep 2018 17:49:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-compare-equality-of-values-within-a-variable-by-subject/m-p/493801#M129990</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-09-08T17:49:40Z</dc:date>
    </item>
    <item>
      <title>Re: How do I compare equality of values within a variable by subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-compare-equality-of-values-within-a-variable-by-subject/m-p/493806#M129994</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id test $ pass $ level $ ;
datalines;
1 a 'Yes' 'hard'
1 b 'Yes' 'hard'
2 a 'Yes' 'hard'
2 b 'Yes' 'medium'
3 a 'Yes' 'medium'
3 b 'No' 'medium'
4 a 'Yes' 'hard'
4 b 'Yes' 'hard'
5 a 'No' 'medium'
5 b 'Yes' 'medium'
;
run;

proc sql;
create table want as
select Id, case when count(distinct pass)&amp;gt;1 or count(distinct level)&amp;gt;1 then 'NO' else 'YES' end as Match
from have
group by id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or the same with ifc&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select Id, ifc(count(distinct pass)&amp;gt;1 or count(distinct level)&amp;gt;1 ,'NO' ,'YES')  as Match
from have
group by id;
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>Sat, 08 Sep 2018 18:07:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-compare-equality-of-values-within-a-variable-by-subject/m-p/493806#M129994</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-09-08T18:07:58Z</dc:date>
    </item>
    <item>
      <title>Re: How do I compare equality of values within a variable by subject</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-compare-equality-of-values-within-a-variable-by-subject/m-p/493883#M130038</link>
      <description>&lt;P&gt;&lt;BR /&gt;data have;&lt;BR /&gt;input id test $ pass $ level $ ;&lt;BR /&gt;datalines;&lt;BR /&gt;1 a 'Yes' 'hard'&lt;BR /&gt;1 b 'Yes' 'hard'&lt;BR /&gt;2 a 'Yes' 'hard'&lt;BR /&gt;2 b 'Yes' 'medium'&lt;BR /&gt;3 a 'Yes' 'medium'&lt;BR /&gt;3 b 'No' 'medium'&lt;BR /&gt;4 a 'Yes' 'hard'&lt;BR /&gt;4 b 'Yes' 'hard'&lt;BR /&gt;5 a 'No' 'medium'&lt;BR /&gt;5 b 'Yes' 'medium'&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt; select id,case when count(distinct catx(' ',pass,level))=1 then 'Yes' else 'No ' end as flag&lt;BR /&gt; from have&lt;BR /&gt; group by id;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Sun, 09 Sep 2018 10:16:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-compare-equality-of-values-within-a-variable-by-subject/m-p/493883#M130038</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-09-09T10:16:59Z</dc:date>
    </item>
  </channel>
</rss>

