BookmarkSubscribeRSS Feed
mglogan
Obsidian | Level 7

Hello,

I need to compare multiple variables to each other when the value is not missing (not populated yet). The values across all variables (in this case there are 3) should all be equal and we want to flag when they are not the same.

 

The challenge is that we do not want to include the missing values in the comparison.

 

Is there an efficient way to do this without the use of multiple IF THEN and/or missing functions?

 

Example data layout:

 

X             |              Y           |              Z             |        FLAG

 

yes         |              yes         |              yes         |

 

no           |                            |              no           |

 

yes         |              no           |                            |            Y

 

               |                            |                             |

6 REPLIES 6
novinosrin
Tourmaline | Level 20

Is it always 'yes','no' or missing of all possible values?

novinosrin
Tourmaline | Level 20
data have;
input (X      Y     Z)   ($) ;
cards;
yes     yes    yes 
no   .        no   
yes     no     .    
;

data want;
set have;
if find(cats(of x--z),'yesno') or find(cats(of x--z),'noyes') then Flag='Y';
run;
ChrisNZ
Tourmaline | Level 20

Like this?

data HAVE;
   X='1'; Y='1'; Z='1'; output;
   X=' '; Y='1'; Z='1'; output;
   X='2'; Y=' '; Z='1'; output;
   X=' '; Y='3'; Z='1'; output;
run;
data WANT; 
  set HAVE;
  ALL=':'||catx(':', of X--Z);   
  FLAG=missing(tranwrd(ALL,':'||scan(ALL,1,':'),''));
run;

X Y Z FLAG
1 1 1 1
  1 1 1
2   1 0
  3 1 0
novinosrin
Tourmaline | Level 20

Okay , I've been made to wake up 

 

data have;
input (X      Y     Z)   ($) ;
cards;
yes     yes    yes 
no   .        no   
yes     no     .    
;

data want;
set have;
k=coalescec(of x--z);
if count(cats(of x--z),strip(k)) ne countw(catx(' ',of x--z)) then flag='Y';
drop k;
run;
ChrisNZ
Tourmaline | Level 20

@novinosrin Your method relies on the data being compliant.

The 3 values no nono yes would pass with no Y flag.

Ksharp
Super User
data have;
input (X      Y     Z)   ($) ;
cards;
yes     yes    yes 
no   .        no   
yes     no     .    
;


data want;
 if _n_=1 then do;
 length temp $ 80;
 declare hash h();
 h.definekey('temp');
 h.definedone();
 end;
set have;
array _x{*} $ _character_;
do i=1 to dim(_x);
 if not missing(_x{i}) then do;temp=_x{i};h.replace();end;
end;
if h.num_items > 1 then flag='Y';
h.clear();
drop i temp;
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 1225 views
  • 0 likes
  • 4 in conversation