Announcement

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

  • Using Local in a Loop

    Hello everyone,

    I'm having a problem commanding a local to do a function. The idea of the whole loop is to match a control firm to a treatment firm, append onto the matched_pairs dataset, and then drop the control firm such that it cannot match to any other treatment firms. The code is shown below:

    Code:
     append using matched_pairs.dta, nolabel
                            
                                                        save matched_pairs.dta, replace
                                                        local abc bncusip == _N
                                                        use matching_firms`i'.dta, replace
                                                        drop if bncusip == abc, force
                                                        save matching_firms`i'.dta, replace
    The issue is with the local command: I want a create a local command abc that identifies the last observation in the bncusip column, so that I can identify that observation in the matching_firms dataset and drop it.

    Can anyone help fixing this local command? Thank you!

  • #2
    The local command is correct; it does not need to be fixed. It is your -drop- command that contains two errors. It should be:
    Code:
    drop if bncusip == `abc'
    1. The quotes around abc are required. Also be careful to use the ` character as the left quote and the ' character as the right quote.

    2. -drop- does not take a -force- option (nor any option at all, for that matter).

    Comment


    • #3
      With the definition of Louis Kos

      Code:
      local abc bncusip == _N
      and the advice of Clyde Schechter

      Code:
      drop if bncusip == `abc'
      the result will be
      Code:
      drop if bncusip == bncusip == _N
      which is, I guess, legal but also not at all what is wanted. I am not sure that I understand the problem, but
      Code:
      local abc = bncusip[_N]
      may impart some improvement.

      Comment


      • #4
        Hi Clyde, Nick,

        Thank you for taking a look at the problem so fast. Using Clyde Schechter 's command, the code seems to function, however, unsuccessfully deletes a line in the "'matching_firms" file after opening up the file. More specifically, the code for the loop is shown below: for every firm in the file "ipo_companies", merge with the "matching_firms" dataset based on date and industry code, drop all non-matches, then pick out the matching firm with the nearest market value. Once picked out, append to a "matched_pairs" file (so the matching firm will be the last line).

        This is where I am aiming to use local to remember the last line appended to the "matched_pairs" dataset based on bncusip, and drop all of the observations in the "matching_firms" dataset that have this bncusip, such that this matching firm cannot match to any other treatment firm.

        Code:
        use "ipo_companies.dta", replace
        
        global N=_N
        
            forvalues n = 1/$N {
         
            use "ipo_companies.dta", replace
            
                keep if _n==`n'
                display `n'
                merge 1:m industrycode date using "matching_firms.dta"
                keep if _merge == 3
                drop _merge
                
                *Pick nearest neighbor according to market distance
                compress
                if _N!=0 {
                    gen n_dist = (abs(marketvalue_control - ipo_price_treatment))
                    sort n_dist
                    keep if _n == 1
                    drop n_dist
                    }
                        if `n' > 1 {
                        append using "matched_pairs.dta", nolabel
        
                        save "matched_pairs.dta", replace
                        local abc = bncusip[_N]
                        use "matching_firms.dta", replace
                        drop if bncusip == `abc'
                        save "matching_firms.dta", replace
                        }
                save "matched_pairs.dta", replace
                }
        Currently, using this new code by Nick Cox , I received an invalid syntax r(198). Is there something I am missing/is incorrectly defined? Thank you very much in advance!
        Last edited by Louis Kos; 18 Feb 2020, 02:39.

        Comment


        • #5
          Which command evokes the error message?

          Comment


          • #6
            Code:
             
             use "ipo_companies.dta", replace
            needs to be changed into
            Code:
             
             use "ipo_companies.dta", clear
            The same goes for the other use-command. You somewhat replace a dataset in memory, when you load it while another dataset exist. But for Stata there is difference between clearing the memory and replacing/overwriting something. Maybe that's where the confusion comes from.

            Comment


            • #7
              Nick Cox the error seems to come after saving the matched_pairs file:

              [CODE] matched_pairs.dta saved
              invalid syntax
              r(198);
              /CODE]

              Which is after the 'append' command. Sven-Kristjan Bormann I have corrected for this - thank you. However, the error persists.

              Comment


              • #8
                The code must be run as a block. Otherwise the local definition is invisible to the code that uses it. I can't think of anything else, but absence of ideas is just what it is (isn't?).

                Comment


                • #9
                  Hi Nick,

                  What do you mean by 'the code must run as a block'? Is this currently not the case with the existing code?

                  To add further insight: when global is used instead of local, this is actually when the invalid syntax r(198) error persists.

                  When local is used, the error type mismatch r(109) error occurs in the same situation (after the matched_pairs.dta file is saved).

                  The renewed code is as shown:

                  Code:
                   use "ipo_companies.dta", replace
                  
                  global N=_N
                  
                      forvalues n = 1/$N {
                   
                      use "ipo_companies.dta", clear
                      
                          keep if _n==`n'
                          display `n'
                          merge 1:m industrycode date using "matching_firms.dta"
                          keep if _merge == 3
                          drop _merge
                          
                          *Pick nearest neighbor according to market distance
                          compress
                          if _N!=0 {
                              gen n_dist = (abs(marketv_ct`i' - ipo_price_tr))
                              sort n_dist
                              keep if _n == 1
                              drop n_dist
                              }
                                  if `n' > 1 {
                                  append using "matched_pairs.dta", nolabel
                                  
                                  save "matched_pairs.dta", replace
                                  local abc = bncusip[_N]
                                  use "matching_firms.dta", clear
                                  drop if bncusip == `abc'
                                  save "matching_firms.dta", replace
                                  }
                          save "matched_pairs.dta", replace
                          }

                  Comment


                  • #10
                    I mean what I say, naturally.... Running code as a block means running an entire do-file or the entire block of code in a do-file editor window in single operation.

                    The opposite -- not running code as a block -- is what users sometimes do, typically out of nervousness or caution, namely execute a line at a time from the do-file editor window.

                    Looking at your code again I stopped at the first line

                    Code:
                     
                      use "ipo_companies.dta", replace
                    which still shows the error pointed out in #6 by Sven-Kristjan Bormann

                    Comment

                    Working...
                    X