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;
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;
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.
Thank you for your reply.
Yes you are absolutely correct. "w2" observation should be compared to "w3" observation.
sorry for my previous post.
it should compare with immediately follow.
here "w2" values should compare with "w3" values.
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.
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.
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;
Thank you so much, you are awesome.
Its working fine.
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;
Thank you so much for your immediate help.
It is working absoultely fine.
Regards,
Raju
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!
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.