Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • help: complex loop to find "x" and then iterate backwards by row

    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 :
    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)
    Hope that makes sense.
    Attached Files
    Last edited by mike p; 08 Feb 2023, 22:06.

  • #2
    My pseudo code has some errors..I tried my hand at writing the actual code, but a) I am not at my STATA machine to test and b) I am not very familiar with STATA's loops. Anyways, here it is:

    Code:
    local N = _N
    forvalues i = 1/`N' {
       if AG[`i']="x" {
          local templevel = level[`i']
          local z = 1
          local b = 0
          while b = 0 {
             if AG[`i'-z] && AG[`i'-z]=="" {
                if templevel <= level[`i'] {
                   replace AG[`i'-z] = "x"
                   local templevel = level[`i'-z]
                   local ++z
                }
             b=1
             }
          }
       }
    }

    Comment


    • #3
      Here is a data example in requested style.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str11 heading str5 var float level
      "Heading"     "h" 0
      "Heading"     ""  0
      "Heading"     ""  0
      "Information" ""  1
      "Heading"     ""  0
      "Information" ""  1
      "Heading"     ""  0
      "Heading"     ""  0
      "Information" ""  1
      "Context"     ""  2
      "Heading"     ""  0
      "Context"     ""  2
      "Heading"     ""  0
      "Context"     ""  2
      "Heading"     ""  0
      "Context"     ""  2
      "Heading"     ""  0
      "Context"     ""  2
      "Heading"     ""  0
      "Context"     ""  2
      "Heading"     ""  0
      "Context"     ""  2
      "Context"     ""  3
      "Heading"     ""  0
      "Heading"     ""  0
      "Context"     ""  2
      "Heading"     ""  0
      "Heading"     ""  0
      "Context"     ""  2
      "Heading"     ""  0
      "Information" ""  1
      "Information" ""  1
      "Information" ""  1
      "Information" ""  1
      "Information" ""  1
      "Heading"     ""  0
      "Heading"     ""  0
      "Heading"     ""  0
      "Context"     ""  3
      "Context"     ""  3
      "Context"     ""  3
      "Context"     ""  3
      "Context"     ""  3
      "Heading"     ""  0
      "Context"     ""  2
      "Context"     ""  3
      "Context"     ""  3
      "Heading"     ""  0
      "Context"     ""  2
      "Context"     ""  3
      "Context"     ""  4
      "Context"     ""  3
      "Context"     ""  3
      "Context"     ""  3
      "Heading"     ""  0
      "Information" ""  1
      "Information" ""  1
      "Heading"     "h" 0
      "Heading"     "h" 0
      "Technical"   "x" 5
      "Technical"   "x" 5
      "Context"     ""  3
      "Heading"     ""  0
      "Context"     ""  2
      "Context"     ""  3
      "Heading"     ""  0
      "Context"     ""  2
      "Context"     ""  3
      "Heading"     ""  0
      "Context"     ""  2
      "Context"     ""  3
      "Context"     ""  3
      "Context"     ""  3
      "Context"     ""  3
      "Context"     ""  3
      "Context"     ""  3
      "Context"     ""  3
      "Heading"     ""  0
      "Heading"     ""  0
      "Heading"     ""  0
      "Context"     ""  2
      "Context"     ""  2
      "Context"     ""  3
      "Context"     ""  3
      "Context"     ""  3
      "Context"     ""  3
      "Context"     ""  2
      "Heading"     ""  0
      "Context"     ""  2
      "Context"     ""  3
      "Heading"     ""  0
      "Context"     ""  2
      "Context"     ""  3
      "Heading"     ""  0
      "Heading"     ""  0
      "Context"     ""  2
      "Context"     ""  3
      "Context"     ""  3
      "Context"     ""  3
      "Heading"     ""  0
      end
      and a variable seqnum could be produced by

      Code:
      gen long seqnum = _n
      In Stata looking at earlier observations is often best done by reversing the order so that they become later observations.

      Comment

      Working...
      X