Fluorite | Level 6

## Checking whether a group of variables has the same value or not

Hi, I would like to check whether a group of variables has the same value for a class or not:

 Group Class Value a 1 b 2 a 2 c 2 d 3 b 2

For example group a has different values (1 and 2) but group b has the same values(2 and 2). How do I check this is SAS?

1 ACCEPTED SOLUTION

Accepted Solutions
Tourmaline | Level 20

## Re: Checking whether a group of variables has the same value or not

Ok. This flags groups with different values within the group.

``````data have;
input Group \$ ClassValue;
datalines;
a 1
b 2
a 2
c 2
d 3
b 2
;

proc sort data = have;
by Group;
run;

data want;

do _N_ = 1 by 1 until (last.Group);
set have;
by Group;
if lag(ClassValue) ne ClassValue and _N_ ne 1 then flag = 1;
end;

do _N_ = 1 to _N_;
set have;
output;
end;

run;``````
11 REPLIES 11
Tourmaline | Level 20

## Re: Checking whether a group of variables has the same value or not

What if group b has 2, 2, and 3? Do you  want to check if two similar numbers appear within the same group or if all numbers within the same group are equal?

Fluorite | Level 6

## Re: Checking whether a group of variables has the same value or not

Hi, I want to check if all numbers within the same group are equal or not (it doesn't matter to me how many different class values a group has).

Tourmaline | Level 20

## Re: Checking whether a group of variables has the same value or not

Ok. This flags groups with different values within the group.

``````data have;
input Group \$ ClassValue;
datalines;
a 1
b 2
a 2
c 2
d 3
b 2
;

proc sort data = have;
by Group;
run;

data want;

do _N_ = 1 by 1 until (last.Group);
set have;
by Group;
if lag(ClassValue) ne ClassValue and _N_ ne 1 then flag = 1;
end;

do _N_ = 1 to _N_;
set have;
output;
end;

run;``````
Fluorite | Level 6

## Re: Checking whether a group of variables has the same value or not

Thank you so much!
Garnet | Level 18

## Re: Checking whether a group of variables has the same value or not

Run proc freq() and check the output report:

``````proc freq data=have;
table group*class / nopercent norow nocol;
run;``````
Fluorite | Level 6

## Re: Checking whether a group of variables has the same value or not

Hi, my data set is too big to make proc freq of interactions feasible, but thanks!
Onyx | Level 15

## Re: Checking whether a group of variables has the same value or not

One more approach:

``````data have;
input Group \$ ClassValue;
datalines;
a 1
b 2
a 2
c 2
d 3
b 2
;
run;
proc sort
data = have(keep=Group ClassValue)
out = have2
nodupkey
;
by Group ClassValue;
run;

data have2;
set have2;
by Group;

if first.group > last.group then output;
keep Group;
run;
proc print;
run;

data want;
declare hash H(dataset:"have2");
H.defineKey("Group");
H.defineDone();

do until(eof);
set have end=eof;
flag = ^H.check();
output;
end;

stop;
run;
proc print;
run;``````

All the best

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation

Super User

## Re: Checking whether a group of variables has the same value or not

``````data have;
input Group \$ ClassValue;
datalines;
a 1
b 2
a 2
c 2
d 3
b 2
;

proc sql;
create table want as
select *,count(distinct classvalue)=1 as Has_same_value
from have
group by group;
quit;``````

## Re: Checking whether a group of variables has the same value or not

I like @Ksharp 's approach.  Here is yet another way using PROC MEANS.

``````data have;
input Group \$ ClassValue;
datalines;
a 1
b 2
a 2
c 2
d 3
b 2
;
proc means data=have noprint;
class group;
var classvalue;
output out=want stddev=stddev;
run;

data want2;
length msg \$50;
set want;
if _type_=0 then delete;
if stddev=0 then msg='No variability in group';
else if stddev=. then msg='Insufficient number of observations in group';
else msg='Variability exists in group';
run;

proc print data=want2;
var msg group;
run;``````

SteveDenham

Super User

## Re: Checking whether a group of variables has the same value or not

Steve,
Are you going to start response sas programming problem ?
Aren't you just cared about STAT questions ?

## Re: Checking whether a group of variables has the same value or not

This particular programming issue is something we have to deal with all the time, so I just de-identified the code and added the messages.  Not a programmer except for very specific data manipulations to get stuff in shape for analysis

SteveDenham

Discussion stats
• 11 replies
• 3831 views
• 6 likes
• 6 in conversation