I need to determine the parent of a value. I have a classification structure that is hierarchical in nature. The logic: for each group, the parent of the class is the first value from an undetermined row above that has a smaller length than the current length. data have;
input group $ class $ class_length;
datalines;
68 1 1
68 11 2
68 1101 4
68 1102 4
68 1103 4 68 12 2 68 1201 4 68 1202 4 27 1 1 27 1101 4
27 2 1
27 2201 4
27 2202 4
; The end result should look something like this: data want;
input group $ class $ class_length parent $;
datalines;
68 1 1 .
68 11 2 1
68 1101 4 11
68 1102 4 11
68 1103 4 11 68 12 2 1 68 1201 4 12 68 1202 4 12 27 1 1 . 27 1101 4 1
27 2 1 .
27 2201 4 2
27 2202 4 2
; I have tried using lag and first but I always run into trouble with the siblings (i.e. [11,12], [1101,1102,1103] etc.). I would super appreciate any help someone could provide. Thanks in advance.
... View more