Announcement

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

  • How to skip merging an empty master dataset in a 500 loops?

    Dear Stata experts,

    I'd like to ask a question about merging datasets in a loop when no observations merged and please see my code here:

    Code:
    forval i = 1/500{
    preserve
    bsample
    
    reg y x1 x2 i.sic3 i.year, vce(robust)
    
    gen b_x1 = _b[x1]
    gen se_x1 = _se[x1]
    gen t_x1 = abs(b_x1 / se_x1)
    
    * keep significant coefficients
    keep if t_x1 >= 1.65
    
    * if no observations left, exit
     if c(rc) != 0 {
      exit (c(rc))
     }
    
     else {
       gen index = `i'
        keep gvkey fyear
        
        merge m:1 gvkey fyear using "base.dta"
        keep if _merge == 3
        drop _merge
    restore
    }
    }
    However, it stops and shows an error message after a few times of merging. I want it to skip merging if no observations left in the master dataset after
    Code:
    keep if t_x1 >= 1.65
    and start another loop, and keep going until 500 times. Is there any advice on how to resolve this issue?


    Code:
        Result                      Number of obs
        -----------------------------------------
        Not matched                        49,576
            from master                         0  (_merge==1)
            from using                     49,576  (_merge==2)
    
        Matched                                 0  (_merge==3)
        -----------------------------------------
    (49,576 observations deleted)
    no observations
    r(2000);
    
    end of do-file


    Many thanks!
    Jae
    Last edited by Jae Li; 22 Apr 2024, 15:12.

  • #2
    To get around the immediate problem with -merge- you can do something like this:
    Code:
    else {
       gen index = `i'
        keep gvkey fyear
        if _N > 0 {
            merge m:1 gvkey fyear using "base.dta"
            keep if _merge == 3
            drop _merge
        }
        restore
    }
    That said, I see some other questionable aspects of your code. After completing the -merge-, your next command is a -restore-, which will obliterate the results that you just created with the -merge-. Don't you want to put a command in there to save the results somewhere?

    Also, further up in the code, -if c(rc) != 0- is pointless there. And, in particular, it does not tell you whether there are any observations left at that point. -c(rc)- is the system variable that contains the return code of the immediately preceding command, which is -keep if t_x1 >= 1.65-. The only way that command can fail is if variable t_x1 does not exist. But if that is the case, Stata will halt before even executing -if c(rc) != 0-. So if you even reach that command at all, c(rc) is necessarily 0. So that -if- guard does nothing at all.

    Finally, just to make the code a bit briefer and easier to follow, the three-command sequence of -merge ...- -keep if _merge == 3- and -drop _merge- can be combined into a single command: -merge m:1 gvkey fyear using base, keep(match) nogenerate-. (Note also that neither the quotes nor the .dta are necessary in specifying the file base.dta here.)

    Comment

    Working...
    X