Recursive SQL Query
A family tree is a relation (Father, Mother, Child). The restriction of this relation to subrelation (Father, Son) or (Mother, Daughter) looks more like a tree in the sense of computer data structure.
It is a quite common question in genealogy to trace the ancestry of a given offspring. The algorithm can be specified recursively in a language agnostic way:
Take the paternal branch of the family tree. The ancestry of a male offspring A is the union of the father of A and the ancestry of the father of A.
How could this question be addressed from within SAS? Consider the following three generation family tree where each individual is coded with an id number:
data FamilyTree; input Father Mother Child;
datalines;
1 2 9
1 2 10
1 2 11
3 4 12
3 4 13
3 4 14
3 4 15
5 6 16
5 6 17
7 8 18
7 8 19
7 8 20
9 12 21
9 12 22
9 12 23
10 16 24
10 16 25
15 18 26
15 18 27
;
run;
The following SAS macro prints out the entire paternal ancestry of a given offspring:
%macro Tracing(OffSpring,
Q=%str(select Father from FamilyTree where Child in (&OffSpring))
);
proc sql;
%unquote(&Q)
;quit;
%if &sqlobs %then
%Tracing(&OffSpring,
Q=%str(select Father from FamilyTree where Child in ( &Q ))
)
;
%mend;
To test, try
%Tracing(21)
The following SAS macro searches and puts the earliest forefather of a given offspring into a macro variable:
%macro ForeFather(Id);
%local Tmp;
proc sql noprint;
select Father into :Tmp from FamilyTree
where Child = &Id;
quit;
%if &sqlobs %then %do;
%ForeFather(&Tmp)
%end;
%else %do;
%global FFather;
%let FFather=&Id;
%end;
%mend;
To test, try
%ForeFather(26)
Reference:
http://tech.groups.yahoo.com/group/sas081708/message/325
Categories
- Best Practices (3)
- Best-Practices (16)
- BioNews (3)
- Business Best Practices (5)
- Case studies (2)
- CDISC (11)
- Clinical Data Management (6)
- Clinical Stories (1)
- Code (13)
- EDC (7)
- Event (3)
- Events (7)
- Menu (3)
- Monthly Contest (12)
- New Technologies (15)
- OpenClinica (2)
- SAS Library (4)
- Scripting (2)
- Tips & Techniques (14)
- Trends (11)




Posted under: 