Contributor
Posts: 38

# Comparing rows

I would like to compare a value between two rows. For e.g. I have duplicate id numbers in Column A, DOB in Column B, and state in Column C.
Id DOB State
xx1 20061023 GA
xx1 20080115 GA
sd45 20050203 FL
sd45 20050203 FL
h5g 20060709 NV
h5g 20080115 NV
7uj 20071026 CA
7uj 20071026 CA

I want to check if DOB for the two (same) ids are same. If they are same I would like to code as 1 else code as 2.

Super Contributor
Posts: 366

## Re: Comparing rows

Hello Rsva,

If I understood you correctly then this is a solution:
[pre]
data i;
input Id \$ DOB State \$;
datalines;
xx1 20061023 GA
xx1 20080115 GA
sd45 20050203 FL
sd45 20050203 FL
h5g 20060709 NV
h5g 20080115 NV
7uj 20071026 CA
7uj 20071026 CA
;
run;
proc sort data=i;
by ID DOB;
run;
data r;
set i;
if First.DOB=LAST.DOB then code=2;
if NOT (First.DOB = 1 and Last.DOB = 1) then code=1;
by ID DOB;
run;
[/pre]
Sincerely,
SPR
Valued Guide
Posts: 2,191

## Re: Comparing rows

Rsva
what should happen if an ID has more than two rows?
peterC
Super Contributor
Posts: 366

## Re: Comparing rows

Hello PeterC,

My code is taking care of these cases too.

SPR
Valued Guide
Posts: 2,191

## Re: Comparing rows

yes but SPR, you are assuming that a trio(or more) should be handled the same as a pair.
(making my own assumptions)
what if the third child is not the same age as the twins within the family ID?
hence, my question to the original poster
Contributor
Posts: 38

## Re: Comparing rows

Thanks for your question Peter. I tried SPR's sample code and included third row for an id. It worked fine. When I added a third row,

Case1 (Coded as 2)
xx1 20061023 GA 2
xx1 20080115 GA 2
xx1 20100115 GA 2

Case2 (Codes twin as 1 and the other as 2)
sd45 20050203 FL 1
sd45 20050203 FL 1
sd45 20041023 FL 2

This is what my expected result was. Sorry for not being clear in my question.

Thanks a lot.
Contributor
Posts: 38

## Re: Comparing rows

Thanks SPR it worked perfectly.
Discussion stats
• 6 replies
• 175 views
• 0 likes
• 3 in conversation