DATA Step, Macro, Functions and more

Compare observations within groups

Accepted Solution Solved
Reply
Contributor
Posts: 28
Accepted Solution

Compare observations within groups

Hi SAS Users,

The problem is how to compare observations within groups. My data set has a column named sub_id, which is the ID number of each subjects. Another column is fol_id, it is ID of each subject visit. Another column is value, it is the value of subject.

now we want to compare w3 values with w2, w4 values with w3. for e.g. if we compare w3 values with w2 then 12 & 14 are the new values so we want to show these to in the output, like this if we compare w4 values with w3 then 15 is the new value so here 15 we want to show in the output.

but don't know how to develop code. Any suggestions and sample codes are highly appreciated.

 

data have;
input sub_id $ fold_id $ value;
datalines;
abc w2 10

abc w2 11
abc w3 10
abc w3 14
abc w3 12
abc w3 11
abc w4 12
abc w4 15

abc w4 10
;

run;


Accepted Solutions
Solution
‎08-24-2016 06:52 AM
Super User
Posts: 6,928

Re: Compare observations within groups

So you want to find values in a new group that were not present as one value in the previous group.

try this:

data want;
set have;
by sub_id fold_id;
retain old_list new_list;
length old_list new_list $500;
if first.sub_id then new_list = '';
if first.fold_id
then do;
  old_list = new_list;
  new_list = '';
end;
if old_list ne ' ' and find(old_list,strip(put(value,best.))) = 0 then output;
new_list = catx(' ',trim(new_list),strip(put(value,best.)));
drop old_list new_list;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 6,928

Re: Compare observations within groups

I can't infer a clear rule from what you posted.

Which "w2" observation should be compared to which "w3" observation? Or are you just comparing those that immediately follow? Or sums of values?

A "want" dataset could be heplful in clarifying your intentions.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 28

Re: Compare observations within groups

Thank you for your reply.

 

Yes you are absolutely correct. "w2" observation should be compared to "w3" observation.

Contributor
Posts: 28

Re: Compare observations within groups

sorry for my previous post.

 

it should compare with immediately follow.

here "w2" values should compare with "w3" values.

Super User
Posts: 6,928

Re: Compare observations within groups

data have;
input sub_id $ fold_id $ value;
datalines;
abc w2 10
abc w2 11
abc w3 10
abc w3 14
abc w3 12
abc w3 11
abc w4 12
abc w4 15
abc w4 10
;
run;

data want;
set have;
by sub_id fold_id;
compval = lag(value);
if first.sub_id or not first.fold_id then compval = .;
run;

This will give you the immediately preceding value whenever fold_id changes within a sub_id group.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 28

Re: Compare observations within groups

Thank you so much for your response.

 

Sorry if I misguide you.

 

The actual qestion is total (not sum) "w2" values should compare with total (not sum) "w3" values.

 

like if we compare "w3" total values with "w2" total values then 12 & 14 values are new (which are not in "w2") so these we need to identify.

Solution
‎08-24-2016 06:52 AM
Super User
Posts: 6,928

Re: Compare observations within groups

So you want to find values in a new group that were not present as one value in the previous group.

try this:

data want;
set have;
by sub_id fold_id;
retain old_list new_list;
length old_list new_list $500;
if first.sub_id then new_list = '';
if first.fold_id
then do;
  old_list = new_list;
  new_list = '';
end;
if old_list ne ' ' and find(old_list,strip(put(value,best.))) = 0 then output;
new_list = catx(' ',trim(new_list),strip(put(value,best.)));
drop old_list new_list;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 28

Re: Compare observations within groups

Thank you so much, you are awesome.

 

Its working fine.

Super User
Posts: 9,671

Re: Compare observations within groups

OK.Assuming there are not missing value for VALUE.


data have;
input sub_id $ fold_id $ value;
datalines;
abc w2 10
abc w2 11
abc w3 10
abc w3 14
abc w3 12
abc w3 11
abc w4 12
abc w4 15
abc w4 10
;
run;
data want;
 array x{99999} _temporary_;
 group+1;
  
 do until(last.fold_id);
  set have;
  by sub_id fold_id;
  if value not in x and group ne 1 then output;
 end;
 
 call missing(of x{*});
 n=0;
 do until(last.fold_id);
  set have;
  by sub_id fold_id;
  n+1;
  x{n}=value;
 end;
 
drop n group ;
run;

Contributor
Posts: 28

Re: Compare observations within groups

Thank you so much for your immediate help.

 

It is working absoultely fine.

 

Regards,
Raju

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 456 views
  • 2 likes
  • 3 in conversation