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?
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;
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?
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).
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;
Run proc freq() and check the output report:
proc freq data=have;
table group*class / nopercent norow nocol;
run;
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
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;
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
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
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!
ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.
Find more tutorials on the SAS Users YouTube channel.