Sample 25968: Using POINT= to trace 'parent-child' lineage in a data set
This code reads a SAS data set and uses the same data set as a lookup table to find the parent/child relationships among the observations. The entire "lineage" is followed until all corresponding observations are found for each parent.
Note:
For SAS 9.1 and above, also see
Create a new variable whose value is unique for groups of 'linked' observations.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
data test;
input child parent;
datalines;
23 11
99 88
77 66
72 68
88 77
66 1
68 55
69 55
70 55
55 56
;
data lineage(keep=hist1-hist5);
/* Read in SAS data set */
set test;
/* Create an array with enough elements to hold the maximum number of */
/* observations that will 'link' together. */
array hist(5);
count=1;
/* Put the value of CHILD variable into the array HIST */
hist(count)=child;
/* Process through the entire data set while the value of COUNT is less */
/* than 4, i.e. the upper boundary of the array minus 1. */
do i=1 to last while (count<dim(hist)-1);
/* Read in SAS data set again. Rename the variables to new names so */
/* they can be compared with the variable names coming in with the first */
/* SET statement. The POINT= option allows you to access each observation */
/* by observation number. */
set test(rename=(child=child1 parent=parent1)) nobs=last point=i;
/* As you step through each observation, compare the value of PARENT to the */
/* value of CHILD1 (which is the new name given to the CHILD variable when */
/* data set is brought in second time). */
if parent=child1 then do;
count+1;
/* populate array with value of CHILD1 */
hist(count)=child1;
child=child1;
parent=parent1;
i=1;
end;
end;
/* increment COUNT */
count+1;
hist(count)=parent;
run;
proc print data=lineage;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Obs hist1 hist2 hist3 hist4 hist5
1 23 11 . . .
2 99 88 77 66 1
3 77 66 1 . .
4 72 68 55 56 .
5 88 77 66 1 .
6 66 1 . . .
7 68 55 56 . .
8 69 55 56 . .
9 70 55 56 . .
10 55 56 . . .
Use POINT= logic to find the parent/child relationships among the observations in a single data set.
Type: | Sample |
Topic: | SAS Reference ==> DATA Step SAS Reference ==> Statements ==> File-handling ==> SET ==> with POINT= SAS Reference ==> Statements ==> File-handling ==> SET Data Management ==> Manipulation and Transformation ==> Combining and Modifying Data Sets
|
Date Modified: | 2006-01-13 03:02:51 |
Date Created: | 2005-08-17 10:41:19 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |