Announcement

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

  • How to save/create data subsets or frames from a loop, so these frames or subsets can be merged?

    Dear Stata community,

    My question in short: does anyone know how to create a loop which automatically creates new unique subsets or frames (which I want to merge later on)?

    For my thesis I use a Difference in Difference method with propensity score matching to test the effect (ATET) of an acquisition by a Private Equity firm on operational performance of target companies. I use propensity score matching based on industry and year of acquisition to find similar control companies.
    To estimate the ATET I need to create multiple subsets/frames to calculate the propensity scores of all industries in corresponding acquisition years.
    As I have 109 treatment companies, I want to create a loop that will save each subset or frame as a distinct name based on industry group and companynumber

    For example: a frame is created with name "frame1(1)" and another "frame3(4)"

    -> So, the first number corresponds to the industry number and the (number) corresponds to the number of the company within the industry
    -> frame1(1) = frame of industry 1 of company 1 within industry 1
    -> frame 3(4) = frame of industry 3 of company 4 within industry 3

    As I do not know how to use a more simplistic Stata command/code to estimate the ATET with propensity score matching, I was wondering if anybody could help me solve this problem?

    Kind regards,
    Roger

  • #2
    frame names need to be legal Stata names, so no parentheses and they cannot begin with a number. Secondly, there is a restriction on the number of frames allowed. For my Stata SE, this is 100 but there may be a way to increase this. The following using the Grunfeld dataset puts each observation into a distinct frame.

    Code:
    webuse grunfeld
    forval company= 1/5{
        forval time=1/10{
            frame put * if company==`company' & time==`time', into(c`company'_t`time')
        }
    }
    
    *RANDOM CHECK
    
    frame change c3_t5
    l
    Res.:

    Code:
    .
    . *RANDOM CHECK
    
    .
    . frame change c3_t5
    
    .
    . l
    
         +--------------------------------------------------+
         | company   year   invest   mvalue   kstock   time |
         |--------------------------------------------------|
      1. |       3   1939     48.1   2256.2    172.6      5 |
         +--------------------------------------------------+

    Comment


    • #3
      I did not know about the restriction of 100 frames. Andrew's response may tell you what you need. The general thing you are doing is also similar to the second example in https://blog.stata.com/2019/09/06/fun-with-frames/ . My own recent question about appending frames may be helpful as well; I learned from the replies to it about frameappend as well as two other user-written commands that append frames. I believe you'll want to append rather than merge all those subset results. One strategy for dealing with the ceiling of 100 frames would be to do a loop within a loop that does all companies within one industry, appends all those for the industry, drops the individual company frames for that industry, repeats for the next industry etc., Then append all the industry result frames into one larger frame.

      Comment


      • #4
        deleting repeated reply due to slow system response.
        Last edited by Pamela Oliver; 18 Jan 2026, 08:17.

        Comment


        • #5
          On the assumptions that the calculations you propose to do in each frame do not require data from any of the other frames, it may be sensible for you to use -runby- to iterate the propensity score calculations. -runby- is written by Robert Picard and me, and is available to SSC. The advantage of -runby- here is that you can write a program that works on all and only the data for a single iteration, eliminating everything else for the purpose of calculating the propensity scores, and then all of the results get put back together at the end. So you might be able to do this without creating any frames at all. -runby- is available from SSC.

          Comment


          • #6
            Originally posted by Pamela Oliver View Post
            I did not know about the restriction of 100 frames.
            See

            Code:
            help limits
            for this.

            Comment


            • #7
              If the limit of 100 frames bites, you always have the option of going old school: see help postfile, and potentially (though not necessarily) use that with temporary files (see help macro for tempfile). There should be no practical limits to the number of those you can create (other than your disk capacity).

              Comment


              • #8
                I would also add that I see nothing in the original post that implies that all of the frames have to be open at one time. This looks very much like something that can be done sequentially, so that the program will not even come close to requiring 100 frames. This is an X-Y problem. X broaches the limit of 100 frames, but Y does not even come close.

                Comment

                Working...
                X