So, this is a bit of a complex loop that I am struggling with...
Essentially I want to create a loop that looks for a value in a column then looks backward (up a row, looping) to find relevant rows of data. Example to illustrate...
You have a word document with nested headings, lets call those levels, starting from 0 (top level heading [1]) to 3 (subheading [1.1.1.1]). Level 4 is the body_text that can exist under any heading/subheading. What i want the loop to do is find the body_text (level 4), look "up" for the closest heading (between 0-3, but doesn't have to be "3" ["n"] ), then up again for the next level "up" ("n-1"), then up again until it reaches level "0". It will need to go up again to see if any further rows with "0" (in this example, multiple top level headings) exist until it reaches a level > "0" indicating that we have departed that specific nested hierarchy. Back to STATA. Each tested level will exist as its own row, and when a higher level is identified, a variable will be marked with "c" to indicate that it is relevant.
Example_data attached. Note: Did not generate the seqnum in the example.
pseudo code example :
Hope that makes sense.
Essentially I want to create a loop that looks for a value in a column then looks backward (up a row, looping) to find relevant rows of data. Example to illustrate...
You have a word document with nested headings, lets call those levels, starting from 0 (top level heading [1]) to 3 (subheading [1.1.1.1]). Level 4 is the body_text that can exist under any heading/subheading. What i want the loop to do is find the body_text (level 4), look "up" for the closest heading (between 0-3, but doesn't have to be "3" ["n"] ), then up again for the next level "up" ("n-1"), then up again until it reaches level "0". It will need to go up again to see if any further rows with "0" (in this example, multiple top level headings) exist until it reaches a level > "0" indicating that we have departed that specific nested hierarchy. Back to STATA. Each tested level will exist as its own row, and when a higher level is identified, a variable will be marked with "c" to indicate that it is relevant.
Example_data attached. Note: Did not generate the seqnum in the example.
pseudo code example :
Code:
(1) Iterate row by row using seqnum=_n
(2) if "x" in var
(3) if "x" or "c" in var in previous row
(4) continue from (1)
(5) else create tempxlevel of "x" which is in level [a number between 1-5]
(6) look at previous row
(7) if level <= tempxlevel
(8) replace var="c" [of current row]
(9) tempxlevel = level
(10) back to 6
(11) else back to (1)

Comment