Announcement

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

  • Looping over pair of local macros

    Dear Statalist users,

    I would like to ask a question regarding looping over a pair of local macros. I have a dataset with dates and firm returns where Date1 belongs to the returns of firm1, Date2 to firm2, and so on. Now I would like to separate this big dataset into smaller chunks and only keep the combination of Date1 & firm1, Date2 & firm2 etc. How do I specify a loop that does exactly that? Thank you.

    HTML Code:
    local date Date1 Date2 Date3 Date4 Date5
    local firm firm1 firm2 firm3 firm4 firm5
    
    
    foreach d of local date {
       foreach f of local firm {
       peserve
       keep `d' `f'
       save data_`d'_`f', replace
       restore 
        }
    }

  • #2
    You need your loops to be parallel, not nested. That means you have a single loop, not two loops. One way to see this is backwards: Nested loops here will give 5 x 5 = 25 possibilities, but you have just 5 that you care about.

    In the code you present peserve should be preserve. Beyond that, your code seems to be abstracted; but if it is taken fairly literally, you need something more like


    Code:
    forval j = 1/5 {
       preserve
       keep Date`j' firm`j'
       save data_`j', replace
       restore
    }


    The local macros are just a distraction here on this interpretation.

    However, I find it hard to believe that your code is close to what you want -- those names imply a very unusual data layout -- but if that pessimistic guess is right, you'll need to abstract less and show us more that is real or realistic about your data.

    Either way, confusion of parallel loops and nested loops is very common here. See https://journals.sagepub.com/doi/pdf...6867X211063415 for one overview.



    Comment


    • #3
      STATA forval/foreach Nested Loops

      I have up to 11 pairs of Eastings/ Northings coordinates per individual, and I want to check if individuals have remained in the same location at each time point (1-11).
      So Eastings1 needs to be compared to Eastings2, E2 to E3 etc to E10 to E11
      And Northings1 needs to be compared to Northings2, N2 toN3 etc

      * Not all individuals have 11 pairs of coordinates (some have fewer).

      Also, the Eastings & Northings coordinates are not always precise, and may vary by say, 50M e.g. ___993 >> ___976 then back to ___993 at the next timepoint.

      I've created a variable StayPut = 0, which I was going to change to 1 if all the Eastings and Northings are close enough.

      Is there a relatively simple and efficient way to do this using Nested loops? (Northings within Eastings) Or am I better off using GIS?

      I have looked, but I can't quite figure out how to combine forval and foreach in a nested loop.

      Apologies if I've missed a post with the same problem.

      Thanks for your help!

      SCE

      Comment


      • #4
        Giving a good answer to your question will be much easier for someone if you would post an example of your data using -dataex-, as suggested in the StataList FAQ: https://www.statalist.org/forums/help#stata, in particular item 12.2

        Among other things, loops may not even be relevant to your question, depending on the layout of your data set.

        Comment


        • #5
          Mike Lacy is bang on here. The question in #3 is consistent with several data structures but there is one interpretation that is easy to imagine.

          If E1 to E11 or Eastings1 to Eastings11 are 11 variables with up to 11 Eastings and N1 to N11 similarly then you can use egen, rowmax() and egen, rowmin() to get the minimum and maximum across replications of each coordinate, and then get the ranges by subtraction, with some judgment needed on what is a tolerable range (you say 50 m).

          No loops are needed, or if you like egen will do the loops you need.

          Comment

          Working...
          X