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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

9 REPLIES 9
Kurt_Bremser
Super User

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.

Raj_C
Obsidian | Level 7

Thank you for your reply.

 

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

Raj_C
Obsidian | Level 7

sorry for my previous post.

 

it should compare with immediately follow.

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

Kurt_Bremser
Super User
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.

Raj_C
Obsidian | Level 7

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.

Kurt_Bremser
Super User

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;
Raj_C
Obsidian | Level 7

Thank you so much, you are awesome.

 

Its working fine.

Ksharp
Super User
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;

Raj_C
Obsidian | Level 7

Thank you so much for your immediate help.

 

It is working absoultely fine.

 

Regards,
Raju

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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