Announcement

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

  • Invalid syntax error after using args command

    I am in the process of replicating the Stata code of some researchers. The code snippet below is from the very beginning of the do file provided by the researchers. In the code, FactorBetasStartFile is a dta file that is being read into memory. It contains a variable called FirmGroup.

    To give an idea of the dta file, it looks something like this:

    Date ; Company_ID ; Returns ; FirmGroup
    31MAR2003 ; 10001 ; 500 ; 1
    30JUN2006 ; 10001 ; 400 ; 1
    31MAR2003 ; 20001 ; 900 ; 2
    30JUN2006 ; 20001 ; 300 ; 2

    So, a given company has some data over a range of dates. The data is sorted by Company_ID and date. And, the FirmGroup variable increases by one each time we have cycled through the range of dates for a given company. I have attached the first few rows of data of the dta file in an Excel file.

    When I run the code below, I get an invalid syntax error when trying to execute the last line of the code (keep if FirmGroup == `FirmGroupNum'). Any idea on why this might be happening?

    Code:
    args FirmGroupNum
    
    clear
    set more off
    
    use FactorBetasStartFile, clear
    
    keep if FirmGroup == `FirmGroupNum'
    Thanks,
    Tasneem
    Attached Files

  • #2
    My guess is that that .do file is not intended to be run by itself, but is to be called by another .do file probably within a loop.

    A .do file a.do can run another .do file b.do by including the line do b.do. Moreover, it can add arguments by typing do b.do something else. The result is that at the beginning of the .do file b.do there are two local macros define `1' containing "something" and `2' containing "else". The args command takes those macros `1' and `2' and renames them to whatever you specify after args. So if you want to run a very long bit of code in a loop, then you make your code easier to read by putting that in a separate .do file, and pass the state of the loop to that .do file.

    If you run that .do file on its own, then the local macro `FirmGroupNum' is not defined so contains nothing, and keep if FirmGroup == `FirmGroupNum' would then read keep if FirmGroup == which is not valid Stata code.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment

    Working...
    X