BookmarkSubscribeRSS Feed
dataMart87
Quartz | Level 8

Is there a way to check if the values of 3 or more numeric variables are all equal or not?

 

data have;
	var1=0;var2=0;var3=0;output;
	var1=1;var2=1;var3=1;output;
	var1=1;var2=1;var3=0;output;
	var1=1;var2=1;var3=.;output;
	var1=1;var2=.;var3=.;output;
	var1=.;var2=.;var3=.;output;
run;
 

rommel_1-1634848162214.png

 

 

2 REPLIES 2
Reeza
Super User
If the RANGE=0 then all values are the same, range is max of series minus the minimum of the series.

if range(of var1-var3)=0 then result='eq';
ballardw
Super User

@dataMart87 wrote:

Is there a way to check if the values of 3 or more numeric variables are all equal or not?

 

data have;
	var1=0;var2=0;var3=0;output;
	var1=1;var2=1;var3=1;output;
	var1=1;var2=1;var3=0;output;
	var1=1;var2=1;var3=.;output;
	var1=1;var2=.;var3=.;output;
	var1=.;var2=.;var3=.;output;
run;
 

rommel_1-1634848162214.png

 

 


What is "am" supposed to represent?

I really dislike character values for yes/no equal/not equal true/false.

SAS will report 1 as "true/yes" and 0 for "false/no" from comparisons.

Assuming that that you "am" means "no values to compare" this is my approach.The test of NMISS is looking to see if all the values are missing and if so overwrites the normal (yes all the values are the same as all missing are the same value) 1 with a missing value.

Note the much simpler data step for getting the values into the data set.

data have;
   input var1 var2 var3;
   eq = (var1=var2=var3);
   if nmiss(of var:) = 3 then eq=.;
datalines;
0 0 0 
1 1 1 
1 1 0 
1 1 . 
1 . . 
. . . 
run;

If I really wanted to see "eq" "ne" or "am" I would write a custom format.

 

Hint on some reasons to use 1/0 coding scheme.

Sum of the variable EQ is the number of 1's (or values all equal)

Mean of EQ is the percent of 1's when any values are present

Max of Eq, if equal to 1 means that at least one record has all equal

Min of Eq if equal to 1 means that all records with at least one non-missing value are all the same; if zero than at least one record has different values; if missing means that all the values for all the records were missing.

Nmiss of Eq would tell you how many records have no values.

 

It takes very little code to get that information:

Proc means data= have min max mean sum nmiss;
   var eq;
run;

Try getting the same information quickly with character values.

 

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
  • 2 replies
  • 1252 views
  • 2 likes
  • 3 in conversation