BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Giraffe123
Fluorite | Level 6

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

 

 

GroupClass Value
a1
b2
a2
c2
d3
b2

 

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
PeterClemmensen
Tourmaline | Level 20

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;

View solution in original post

11 REPLIES 11
PeterClemmensen
Tourmaline | Level 20

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?

Giraffe123
Fluorite | Level 6

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).

PeterClemmensen
Tourmaline | Level 20

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;
Shmuel
Garnet | Level 18

Run proc freq() and check the output report:

proc freq data=have;
  table group*class / nopercent norow nocol;
run;
Giraffe123
Fluorite | Level 6
Hi, my data set is too big to make proc freq of interactions feasible, but thanks!
yabwon
Onyx | Level 15

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



Ksharp
Super User
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;
SteveDenham
Jade | Level 19

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

Ksharp
Super User
Steve,
Are you going to start response sas programming problem ?
Aren't you just cared about STAT questions ?
SteveDenham
Jade | Level 19

@Ksharp 

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is ANOVA?

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.

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