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

Dear all,

I have a table containing birthdays for a patients. Because this table is the combination of more tables, I have more columns with birthday. for example diagnosis bithday, therapie birthday, follow-up birthday and so on (up to about 8 birthdays)

 

I wish to compare these birthdays. If about let say seven birthdays are the same and only one is different then I will like to mark than column as maybe 1 and probably eliminate the odd in the next step

data test;
set test1;
if column1 ^= column2 and column2 ^= column3 and column3^= column4 
and column4 ^= column5 and column5 ^= column6 and column6 ^= column7
and column7 ^= column8 then col_not_equal =1; run;

using  a code like this is not elegant. Is there an elegant way to do this?

 

best regards

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

First of all, your code will only work if NO two dates are equal, because you used AND instead of OR.

I would use an array over date columns 2 to n, and run a do loop over it and compare it to the first:

array dates {*} column2-column7;
flag = 0;
do i = 1 to dim(dates);
  if dates{i} ne column1 then flag = 1;
end;

View solution in original post

8 REPLIES 8
Kurt_Bremser
Super User

First of all, your code will only work if NO two dates are equal, because you used AND instead of OR.

I would use an array over date columns 2 to n, and run a do loop over it and compare it to the first:

array dates {*} column2-column7;
flag = 0;
do i = 1 to dim(dates);
  if dates{i} ne column1 then flag = 1;
end;
Anita_n
Pyrite | Level 9
@Kurt_Bremser, thanks for the code. I will try thank now and see
Anita_n
Pyrite | Level 9

@Kurt_Bremser: is it also possible to compare data in rows with each other using this method
pat_id  fol_up_id  birthday
1         0  10/01/67
1       1    10/01/67

1      3     11/10/76
1    11    10/01/67

Kurt_Bremser
Super User

Similar method. Keep the first value in a retained variable, and compare to the others.

data want;
set have;
by pat_id;
retain _date flag;
if first.pat_id
then do;
  _date = date;
  flag = 0;
end;
else if date ne _date then flag = 1;
if last.pat_id; /* if you only want one observation per id */
drop _date;
run;
Anita_n
Pyrite | Level 9
Thanks a lot for that
PaigeMiller
Diamond | Level 26

In your problem, does the year of the birthdays have to match all the other years, or do you want just the month-day (but not year) to match across all the birthdays?

--
Paige Miller
Anita_n
Pyrite | Level 9

actually all, day, month and year

mkeintz
PROC Star

This is a use case for MIN and MAX functions.

 

If all the birthdate vars are column1,column2, ... (i.e. the birthdates, and only the birthdates, all begin with "column"), then:

data test;
  set test1;
  if min(of column:)^=max(of column:) then flag=1;
run;

But if the varnames are not so neatly named, then:

data test;
  set test1;
  if min(bdatex, datebd, otherdate, ...) ^= max(bdatex, datebd, otherdate,...) then flag=1;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 825 views
  • 4 likes
  • 4 in conversation