<?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: Check if multiple variables have same value ignoring variables that are empty in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Check-if-multiple-variables-have-same-value-ignoring-variables/m-p/900538#M355903</link>
    <description>&lt;P&gt;You can sort the character variables var1 through var4, by declaring them as part of an array, and using SORTC.&amp;nbsp; Blanks will sort low.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the first non-blank is identical to the last non-blank (and you have 2 or more non-blanks) then all your non-blanks are the same.&amp;nbsp; Use that info to make your new variable, and re-read the observation to unsort the var1-var4 values:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have;

  array v {*} $ var1-var4;
  call sortc(of v{*});
  do i=1 to 4 until(v{i}^=' ');  end;  /*Find first non-blank */
  n_nonblank=5-i;
  if n_nonblank&amp;lt;=1 then all_the_same='N.A.';  /*Not Applicable */
  else if v{i}=v{4} then all_the_same='Yes';  /*First non-blank equals last */
  else all_the_same='No';

  set have;   /*Re-read var1-var4 to undo the sort */
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 29 Oct 2023 21:58:59 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2023-10-29T21:58:59Z</dc:date>
    <item>
      <title>Check if multiple variables have same value ignoring variables that are empty</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-multiple-variables-have-same-value-ignoring-variables/m-p/900530#M355897</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just wanted to see if there is a straight-forward way to do this without having a ton of if-then statements.&amp;nbsp; I have a dataset like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Observation 1:&lt;/P&gt;
&lt;P&gt;Var1 =S&lt;/P&gt;
&lt;P&gt;Var2 = D&lt;/P&gt;
&lt;P&gt;Var3 = S&lt;/P&gt;
&lt;P&gt;Var4 = &amp;lt;missing value&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Observation 2:&lt;/P&gt;
&lt;P&gt;Var1 =&amp;lt;missing value&amp;gt;&lt;/P&gt;
&lt;P&gt;Var2 = D&lt;/P&gt;
&lt;P&gt;Var3 = D&lt;/P&gt;
&lt;P&gt;Var4 = &amp;lt;missing value&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In my dataset there can be every combination of 'S', 'D' or empty values among the four variables.&amp;nbsp; What I need to do, row by row, is check if the populated variables are equal and ignore the variables that are empty .&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I started writing code with if-then statements that checks if var1 ne '' and var2 eq '' and var3 eq '' and var4 eq ''....blah blah blah but I'm going to have to check for every possible combination of populated/empty vars and it's getting too long.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there an easier way to do this?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 29 Oct 2023 19:40:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-multiple-variables-have-same-value-ignoring-variables/m-p/900530#M355897</guid>
      <dc:creator>biglerc</dc:creator>
      <dc:date>2023-10-29T19:40:17Z</dc:date>
    </item>
    <item>
      <title>Re: Check if multiple variables have same value ignoring variables that are empty</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-multiple-variables-have-same-value-ignoring-variables/m-p/900532#M355898</link>
      <description>&lt;P&gt;You need to show us what the result looks like. Such as show what you want for the cases when variables are equal.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, are your values actually single character as shown or did you simplify. This may be an important difference as there is a function COUNTC that works with single characters:&lt;/P&gt;
&lt;PRE&gt;data example;
   string= 'abdssdeabd';
   numa = countc(string,'a');
   numb = countc(string,'b');
   numd = countc(string,'d');
   nums = countc(string,'s');
run;&lt;/PRE&gt;
&lt;P&gt;and it is easy to make a string that doesn't contain spaces to worry about: cats(var1,var2,var3,var4);&lt;/P&gt;</description>
      <pubDate>Sun, 29 Oct 2023 20:16:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-multiple-variables-have-same-value-ignoring-variables/m-p/900532#M355898</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-10-29T20:16:07Z</dc:date>
    </item>
    <item>
      <title>Re: Check if multiple variables have same value ignoring variables that are empty</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-multiple-variables-have-same-value-ignoring-variables/m-p/900533#M355899</link>
      <description>&lt;P&gt;Thank you for the reply!&amp;nbsp; I'm sorry my example didn't provide enough information - I've attached an Excel file with a portion of my dataset so you can see exactly what I'm working with.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My task if to check if all values populated in var1-var4 match (ignoring blanks).&amp;nbsp; If populated values match then the Result is 'Reviewer'.&amp;nbsp; If any of them are different then the Result is 'Chair'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks again!&lt;/P&gt;</description>
      <pubDate>Sun, 29 Oct 2023 20:25:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-multiple-variables-have-same-value-ignoring-variables/m-p/900533#M355899</guid>
      <dc:creator>biglerc</dc:creator>
      <dc:date>2023-10-29T20:25:03Z</dc:date>
    </item>
    <item>
      <title>Re: Check if multiple variables have same value ignoring variables that are empty</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-multiple-variables-have-same-value-ignoring-variables/m-p/900534#M355900</link>
      <description>&lt;P&gt;Are you literally look for those two character values? Or are S and D short had for some longer string?&amp;nbsp; Or perhaps the variables are actually numeric?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And what exactly are trying to detect?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to find the observations where there are DIFFERENT values.&amp;nbsp; Like your first observations then test if both values appear.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;different = indexc(cats(of var1-var4),'S') and indexc(cats(of var1-var4),'D');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or if S and D are really something longer like 'SAM' and "DOUG' then perhaps use WHICHC()?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;different = whichc('SAM',of var1-var4) and whichc('DOUG', of var1-var4);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to find cases where S appear more than once you could use WHICHC() for that also.&amp;nbsp; Look for cases where you find a different variable when searching from left than from the right.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;multiple_S = whichc('S', of  var1-var4) ne 5-whichc('S', of var4-var1);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Do the same thing to detect multiple values of 'D'.&lt;/P&gt;</description>
      <pubDate>Sun, 29 Oct 2023 20:35:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-multiple-variables-have-same-value-ignoring-variables/m-p/900534#M355900</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-10-29T20:35:16Z</dc:date>
    </item>
    <item>
      <title>Re: Check if multiple variables have same value ignoring variables that are empty</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-multiple-variables-have-same-value-ignoring-variables/m-p/900535#M355901</link>
      <description>&lt;P&gt;Hi Tom,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for your reply!&amp;nbsp; My initial example of 'S' and 'D' were just me trying to give a simplified example to see if someone could just help me with how the logic would go.&amp;nbsp; Attached is some actual data and what my result needs to be, if this helps.&amp;nbsp; My task is to check if all populated variables in var1-var4 are equal or not.&amp;nbsp; If they are equal then my result should be 'Reviewer'.&amp;nbsp; If Any differ than my result should be 'Chair'.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Sun, 29 Oct 2023 20:45:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-multiple-variables-have-same-value-ignoring-variables/m-p/900535#M355901</guid>
      <dc:creator>biglerc</dc:creator>
      <dc:date>2023-10-29T20:45:08Z</dc:date>
    </item>
    <item>
      <title>Re: Check if multiple variables have same value ignoring variables that are empty</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-multiple-variables-have-same-value-ignoring-variables/m-p/900536#M355902</link>
      <description>&lt;P&gt;PS it is also important I note that var1 may be empty as well&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for your help, this is saving me!&lt;/P&gt;</description>
      <pubDate>Sun, 29 Oct 2023 21:33:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-multiple-variables-have-same-value-ignoring-variables/m-p/900536#M355902</guid>
      <dc:creator>biglerc</dc:creator>
      <dc:date>2023-10-29T21:33:48Z</dc:date>
    </item>
    <item>
      <title>Re: Check if multiple variables have same value ignoring variables that are empty</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-multiple-variables-have-same-value-ignoring-variables/m-p/900538#M355903</link>
      <description>&lt;P&gt;You can sort the character variables var1 through var4, by declaring them as part of an array, and using SORTC.&amp;nbsp; Blanks will sort low.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the first non-blank is identical to the last non-blank (and you have 2 or more non-blanks) then all your non-blanks are the same.&amp;nbsp; Use that info to make your new variable, and re-read the observation to unsort the var1-var4 values:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have;

  array v {*} $ var1-var4;
  call sortc(of v{*});
  do i=1 to 4 until(v{i}^=' ');  end;  /*Find first non-blank */
  n_nonblank=5-i;
  if n_nonblank&amp;lt;=1 then all_the_same='N.A.';  /*Not Applicable */
  else if v{i}=v{4} then all_the_same='Yes';  /*First non-blank equals last */
  else all_the_same='No';

  set have;   /*Re-read var1-var4 to undo the sort */
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 29 Oct 2023 21:58:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-multiple-variables-have-same-value-ignoring-variables/m-p/900538#M355903</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-10-29T21:58:59Z</dc:date>
    </item>
    <item>
      <title>Re: Check if multiple variables have same value ignoring variables that are empty</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-multiple-variables-have-same-value-ignoring-variables/m-p/900539#M355904</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;This worked perfectly, THANK YOU, I appreciate your help and I learned something today!!!&lt;/P&gt;</description>
      <pubDate>Sun, 29 Oct 2023 22:21:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-multiple-variables-have-same-value-ignoring-variables/m-p/900539#M355904</guid>
      <dc:creator>biglerc</dc:creator>
      <dc:date>2023-10-29T22:21:16Z</dc:date>
    </item>
    <item>
      <title>Re: Check if multiple variables have same value ignoring variables that are empty</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-multiple-variables-have-same-value-ignoring-variables/m-p/900544#M355907</link>
      <description>&lt;P&gt;Below another coding option that doesn't use call sortc() and though won't change the values in your source variables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile datalines truncover dsd;
  input (var1 var2 var3 var4 RESULT) (:$50.);
  datalines;
SD - 03-AUG-2018,SD - 03-AUG-2018,SD - 03-AUG-2018,,Reviewer
NE - 07-SEP-2018,,NE - 07-SEP-2018,,Reviewer
SD - 08-AUG-2019,SD - 08-AUG-2019,SD - 08-AUG-2019,,Reviewer
CR - 12-SEP-2019,SD - 12-SEP-2019,SD - 12-SEP-2019,,Chair
,,,,
,CR - 12-SEP-2019,,NE - 07-SEP-2018,Chair
,NE - 07-SEP-2018,,NE - 07-SEP-2018,Reviewer
,,NE - 07-SEP-2018,,Reviewer,,
CR - 12-SEP-2019,,,NE - 07-SEP-2018,Chair
;

data want(drop=_:);
  set have;
  length result_derived $8 _first_nonblank $50;
  array _a_vars {*} var1 - var4;

  _first_nonblank=coalescec(of _a_vars[*]);

  if not missing(_first_nonblank) then
    do;
      result_derived='Reviewer';
      do _i=2 to dim(_a_vars);
        if not missing(_a_vars[_i]) and _first_nonblank ne _a_vars[_i] then
          do;
            result_derived='Chair';
            leave;
          end;
      end;
    end;

run;

proc print data=want;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or below a variant of above logic that avoids comparing a value with itself in case the&amp;nbsp; first array element is missing.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=_:);
  set have;
  length result_derived $8;
  array _a_vars {*} var1 - var4;

  do _i=2 to dim(_a_vars);
    if missing(_k) and not missing(_a_vars[_i-1]) then _k=_i-1;
    else
    if not missing(_k) then 
      if _a_vars[_k] ne _a_vars[_i] then
      do;
        result_derived='Chair';
        leave;
      end;
  end;
  if missing(result_derived) and not missing(_k) then result_derived='Reviewer';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 29 Oct 2023 23:21:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-multiple-variables-have-same-value-ignoring-variables/m-p/900544#M355907</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-10-29T23:21:35Z</dc:date>
    </item>
  </channel>
</rss>

