Announcement

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

  • Error r(111)

    Hi,

    I just started to learn Stata today and I can`t get my do.file to run.

    I am trying to run http://masteringmetrics.com/wp-conte...9_hicompare.do

    and I get the error code r(111):

    . u NHIS2009_clean, clear

    .
    . * select non-missings
    . keep if marradult==1 & perweight!=0
    (50,662 observations deleted)

    . by serial: egen hi_hsb = mean(hi_hsb1)
    (207 missing values generated)

    . keep if hi_hsb!=. & hi!=.
    (207 observations deleted)

    . by serial: egen female = total(fml)

    . keep if female==1
    (31 observations deleted)

    . drop female

    .
    . * Josh's sample selection criteria
    . gen angrist = ( age>=26 & age<=59 & marradult==1 & adltempl>=1 )

    . keep if angrist==1
    (9,613 observations deleted)

    . // drop single-person HHs
    . by serial: gen n = _N

    . keep if n>1
    (1,331 observations deleted)

    .
    . * count of husbands by HI status
    . tab hi if fml==0 [ aw=perweight ]

    hi | Freq. Percent Cum.
    ------------+-----------------------------------
    0 | 1,281.4929 13.64 13.64
    1 | 8,113.5071 86.36 100.00
    ------------+-----------------------------------
    Total | 9,395 100.00

    .
    . * mean comparisons for husbands
    . foreach var in hlth nwhite age yedu famsize empl inc {
    2. qui reg `var' hi if fml==0 [ w=perweight ], robust
    3.
    . * means and standard devs
    . qui sum `var' if hi==0 & fml==0 [ aw=perweight ]
    4. local m0 =r(mean)
    5. local sd0 =r(sd)
    6. qui sum `var' if hi==1 & fml==0 [ aw=perweight ]
    7. local m1 =r(mean)
    8. local sd1 =r(sd)
    9.
    . if "`var'"=="hlth" {
    10. outreg2 hi using hicompare_hsb, replace excel noaster adec(2) dec(2
    > ) bdec(2) sdec(2) ///
    > addstat("Mean no HI",`m0',"SD no HI",`sd0',"Mean HI",`m1',"SD HI",`sd1
    > ')
    11. }
    12. if "`var'"!="hlth" {
    13. outreg2 hi using hicompare_hsb, append excel noaster adec(2) dec(2)
    > bdec(2) sdec(2) ///
    > addstat("Mean no HI",`m0',"SD no HI",`sd0',"Mean HI",`m1',"SD HI",`sd1
    > ')
    14. }
    15. }
    hicompare_hsb.xml
    dir : seeout
    hicompare_hsb.xml
    dir : seeout
    hicompare_hsb.xml
    dir : seeout
    hicompare_hsb.xml
    dir : seeout
    hicompare_hsb.xml
    dir : seeout
    hicompare_hsb.xml
    dir : seeout
    SD not found
    r(111);

    end of do-file

    r(111);

    To access the data file: http://masteringmetrics.com/resources/ (Table 1.1).

    If anyone could help me out, I would be really grateful.

  • #2
    Welcome to Stata and Statalist. I downloaded the do-file and dataset and I can confirm the error when used with latest version of outreg2 (from SSC).

    The error occurs in the "mean comparisons for husbands" loop while processing the inc variable, at the second outreg2 command:
    Code:
    if "`var'"!="hlth" {
        outreg2 hi using hicompare_hsb, append excel noaster adec(2) dec(2) bdec(2) sdec(2) ///
        addstat("Mean no HI",`m0',"SD no HI",`sd0',"Mean HI",`m1',"SD HI",`sd1') 
    }
    The command is syntactically correct (even though the help for outreg2 does not use double quotes) and handles the previous 6 variables without errors.

    The error occurs because of a bug in outreg2 that fails to correctly parse the addstat(...) part of the command when the value of a statistic is >= 1000. The default format that outreg2 uses across the board is fmt(fc) (general with commas for thousands). When the value of the statistic to add is >= 1000, outreg2 applies the default format to all values specified in addstat(...) and then tries to parse the sub-command again. The do-file's code
    Code:
    addstat("Mean no HI",`m0',"SD no HI",`sd0',"Mean HI",`m1',"SD HI",`sd1')
    with local macro expansion becomes, for variable inc:
    Code:
    addstat("Mean no HI",45656.24603693267,"SD no HI",36305.07640259092,"Mean HI",106466.6881288486,"SD HI",54243.39168654153)
    which is then preprocessed by outreg2 to become:
    Code:
    addstat(Mean no HI,45,656.25,SD no HI,36,305.08,Mean HI,106,466.69,SD HI,54,243.39)
    Since outreg2 uses commas to separate the labels and values, the first (label, value) pair is
    Code:
    Mean no HI,45
    and the second pair is
    Code:
    656.25,SD no HI
    In outreg2's code, this exands to:
    Code:
    local value=SD no HI
    which leads to the reported error:
    Code:
    SD not found
    r(111);
    A simple workaround for this do-file is to add an afmt(g) option to all outreg2 calls:
    Code:
    outreg2 hi using hicompare_hsb, append excel noaster adec(2) afmt(g) dec(2) bdec(2) sdec(2) ///
    addstat("Mean no HI",`m0',"SD no HI",`sd0',"Mean HI",`m1',"SD HI",`sd1')
    and the do-file will terminate without error.

    Comment

    Working...
    X