Announcement

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

  • r(198) error

    I'm trying to use prior code to create a Turnip plot in STATA 11.2. I keep getting a r(198) error when running the code. It seems to get caught up when I'm setting my resolution. It doesn't like `resolut' == 0 ... any advice? Thank you.

    *! Version 1.0.0 (STB-58: gr45)
    *turnip.do capture program drop turnip
    program define turnip
    version 6.0

    syntax varlist(min=1 max=1) [if] [, RESolution(real 0) Truev(real -0.000000001) *]

    f index("`options'", "by(") !=0 {
    di ""
    di "* * * BY option is not allowed * * *"
    di ""
    di " Instead, use the IF option -- set YLABEL and YSCALE "
    di" to ensure identical sizing"
    di ""
    exit
    }

    set more 1

    /* TURNIP generator eg, turnip mpg [if foreign==1] [,RESolution(0)] Truev(1) [ylabel(10 20 30 40 50)] [xlabel(-30 0 30)] [yline(0) TItle(passthru)]

    */ *local ttt= "ttt" + substr("`varlist'", 1, 4)
    tempvar ttt

    *default resolution if `resolut' == 0 {
    quietly sum `varlist'
    local resolut = r(sd)/2.5
    }

    if `resolut'>=0 {
    quietly gen `ttt '= round(`varlist',`resolut')
    }

    else {
    quietly gen `ttt' = `varlist'
    /* suspend all rounding */
    }


    /*TRUEV(alue) option: Suspends rounding of certain values (e.g., only let true 0's appear as 0's...everyone else gets rounded to the nearest category) */ if `truev'~= -0.000000001{ local faker=int(`truev'/`resolut') local i=1 while `i'<_N { local rttt=round(`ttt'[`i'], .000001) local rtruev=round(`truev',.000001) if `rttt'==`rtruev'& `varlist'[`i']>`ttt'[`i'] { quietly replace `ttt'=(`faker'+1)*`resolut' in `i'} if `rttt'==`rtruev'& `varlist'[`i']<`ttt'[`i'] { quietly replace `ttt'=(`faker'-1)*`resolut' in `i'} local i = `i' + 1 } } *calculate x-axis coordinates tempvar horiz quietly gen `horiz'=. sort `ttt' preserve capture keep `if' /* this will allow for -if- option*/ *at each level of ttt, create spacing variable called `horiz' , symmetric around 0 quietly by `ttt':quietly replace `horiz' =round(_n/2,1) -.5 quietly by `ttt':quietly replace `horiz' =-1*`horiz' if (mod(_n,2)==1) *center turnip rows with an odd number of members by moving them over by .5 tempvar h_ct quietly egen `h_ct'=count(`ttt'), by(`ttt') quietly replace `horiz' =`horiz' + .5 if (mod(`h_ct',2)==1) */ *to get n per graph row: **tab `ttt'** and look at obs number * di "# of observations per turnip row is the frequency below:" label var `ttt' "`varlist'" tab `ttt' *default xlabel if index("`options'", "xlabel")<1 { quietly sum `horiz' local defx=round(2*r(max), 1) local ndefx=round(-1*`defx', 1) local options = "`options'" + " xlabel(`ndefx' `defx')" } *default ylabel if index("`options'", "ylabel")<1 { quietly sum `ttt' local fixy = (r(max) - r(min))*0.1 local defy= round((r(min) - `fixy'), 1) local ndefy=round((r(max) + `fixy'), 1) local options = "`options'" + " ylabel(`defy' `ndefy' )" } local dflag="none" local d="" *yline mean option local a=index("`options'", "yline(mean)") if `a'!=0{ local dflag="mean" quietly sum `varlist' /*use varlist not tttvarlist ! */ local b=`a'+ 5 local c = `a' + 10 local d = _result(3) local f = length("`options'") local options = substr("`options'", 1, `b') + substr("`d'", 1, 4) + /* */ substr("`options'", `c', `f') * now add mean to ylabel local a=index("`options'", "ylabel(") if `a' > 0 { local b = `a' + 6 local c = `a' + 7 local f = length("`options'") local options = substr("`options'", 1, `b') + " `d' " /* */ + substr("`options'", `c', `f') } } *yline median option local a=index("`options'", "yline(median)") if `a'!=0{ local dflag="median" quietly sum `varlist', det /*use varlist not tttvarlist ! */ local b=`a'+ 5 local c = `a' + 12 local d = _result(10) local f = length("`options'") local options = substr("`options'", 1, `b') + substr("`d'", 1, 4) + /* */ substr("`options'", `c', `f') * now add mean to ylabel local a=index("`options'", "ylabel(") if `a' > 0 { local b = `a' + 6 local c = `a' + 7 local f = length("`options'") local options = substr("`options'", 1, `b') + " `d' " /* */ + substr("`options'", `c', `f') } } di "options: `options'" di "resolution: `resolut'" if "`dflag'" != "none" {di "yline: `d' (`dflag')"} di " " graph `ttt' `horiz' , `options' b2title(" ") l1title("`varlist'") xtick(0) restore end
    Last edited by Mike Ericson; 01 Dec 2018, 20:47.

  • #2
    This is rather old code. But even version control doesn't protect you from what is long since considered an error. The macro concerned is created as resolution: you refer to resolut and there is no such macro.

    A long time ago you could abbreviate local macro references down to the first 7 letters of each name. Or equivalently in practice you could write longer names that made more sense; Stata just ignored characters 8 on. I don't think that was ever documented. I remember one Stata developer telling me that he was surprised to find you could do that: it was an accidental side-effect of some other part of the internal code. But, no matter, you can't now -- or even in Stata 11.2 --- abbreviate local macro names.

    Your code would be much, much more readable if you used CODE delimiters as we do request in the FAQ Advice. Please note #12 and also #18 in that place.

    Out of curiosity I downloaded the code. I needed to fix this error and one other (by modern standards) but then this code

    Code:
    sysuse auto, clear
    turnip mpg
    worked in Stata 15.1 and should I think also work in 11.2. Here's the full program:

    Code:
    *! 1.0.1 NJC 2 December 2018 to "work" in 15.1
    *! Version 1.0.0   (STB-58: gr45)
    *turnip.do
    capture program drop turnip
    program define turnip
    version 6.0
    
    syntax varlist(min=1 max=1) [if] [, RESolution(real 0) Truev(real -0.000000001) *]
    
    if index("`options'", "by(") !=0 {
        di ""
        di "*  *  *    BY option is not allowed    *  *  *"
        di ""
        di "    Instead, use the IF option -- set YLABEL and YSCALE "
        di"              to ensure identical sizing"
        di ""
        exit
    }
    
    set more 1
    
    
    /*  TURNIP generator
    
    eg,        turnip mpg [if foreign==1] [,RESolution(0)] Truev(1) [ylabel(10 20 30 40 50)]  [xlabel(-30 0 30)]  [yline(0) TItle(passthru)]
    
    
    */
    
    *local ttt= "ttt" + substr("`varlist'", 1, 4)
    tempvar ttt
    
    *default resolution
    if `resolution' == 0 {
        quietly sum `varlist'
        local resolution = r(sd)/2.5
    }
    
    if `resolution'>=0 {
        quietly gen `ttt '= round(`varlist',`resolution')
        }
    else {
        quietly gen `ttt' = `varlist'
        /*  suspend all rounding  */
    }
    
    
    
    /*TRUEV(alue) option:  Suspends rounding of certain values (e.g., only let true 0's appear as 0's...everyone else gets rounded to the nearest category)    */
    if `truev'~= -0.000000001{
        local faker=int(`truev'/`resolution')
        local i=1
        while `i'<_N {
            local rttt=round(`ttt'[`i'], .000001)
            local rtruev=round(`truev',.000001)
            if `rttt'==`rtruev'& `varlist'[`i']>`ttt'[`i'] {
                quietly replace `ttt'=(`faker'+1)*`resolution' in `i'}
            if `rttt'==`rtruev'& `varlist'[`i']<`ttt'[`i'] {            
                quietly replace `ttt'=(`faker'-1)*`resolution' in `i'}
            local i = `i' + 1
        }
    }
    
    
    *calculate x-axis coordinates
    tempvar horiz
    quietly gen `horiz'=.
    
    
    sort `ttt'
    
    preserve
    capture keep `if'    /* this will allow for -if- option*/
    
    *at each level of ttt, create spacing variable called `horiz' , symmetric around 0
    quietly by `ttt': replace `horiz' =round(_n/2,1) -.5
    quietly by `ttt': replace `horiz' =-1*`horiz' if (mod(_n,2)==1)
    
    *center turnip rows with an odd number of members by moving them over by .5
    tempvar h_ct
    quietly egen `h_ct'=count(`ttt'), by(`ttt')
    quietly replace `horiz' =`horiz' + .5 if (mod(`h_ct',2)==1)
    */
    
    *to get n per graph row:   **tab `ttt'**  and look at obs number *
    di "# of observations per turnip row is the frequency below:"
    label var `ttt' "`varlist'"
    tab `ttt'
    
    *default xlabel
    if index("`options'", "xlabel")<1 {
        quietly sum `horiz'
        local defx=round(2*r(max), 1)
        local ndefx=round(-1*`defx', 1)
        local options = "`options'" + " xlabel(`ndefx'  `defx')"
    }
    
    *default ylabel
    if index("`options'", "ylabel")<1 {
        quietly sum `ttt'
        local fixy = (r(max) - r(min))*0.1
        local defy= round((r(min) - `fixy'), 1)
        local ndefy=round((r(max) + `fixy'), 1)
        local options = "`options'" + " ylabel(`defy' `ndefy' )"
    }
    
    
    local dflag="none"
    local d=""
    
    *yline mean option
    local a=index("`options'", "yline(mean)")
    if `a'!=0{
        local dflag="mean"
        quietly sum `varlist'    /*use varlist not tttvarlist ! */
        local b=`a'+ 5
        local c = `a' + 10
        local d = _result(3)
        local f = length("`options'")
        local options = substr("`options'", 1, `b') + substr("`d'", 1, 4) + /*
            */        substr("`options'", `c', `f')
    
    *    now add mean to ylabel
    
        local a=index("`options'", "ylabel(")
        if `a' > 0 {
            local b = `a' + 6
            local c = `a' + 7
            local f = length("`options'")
            local options = substr("`options'", 1, `b') + " `d' "     /*
                */   + substr("`options'", `c', `f')
        }
    }    
    
    *yline median option
    local a=index("`options'", "yline(median)")
    if `a'!=0{
        local dflag="median"
        quietly sum `varlist', det    /*use varlist not tttvarlist ! */
        local b=`a'+ 5
        local c = `a' + 12
        local d = _result(10)
        local f = length("`options'")
        local options = substr("`options'", 1, `b') + substr("`d'", 1, 4) + /*
            */        substr("`options'", `c', `f')
    
    *    now add mean to ylabel
    
        local a=index("`options'", "ylabel(")
        if `a' > 0 {
            local b = `a' + 6
            local c = `a' + 7
            local f = length("`options'")
            local options = substr("`options'", 1, `b') + " `d' "     /*
                */   + substr("`options'", `c', `f')
        }
    }    
    
    
    di "options:  `options'"
    di "resolution:  `resolution'"
    if "`dflag'" != "none" {di "yline:  `d'  (`dflag')"}
    di " "
    graph `ttt' `horiz' ,   `options' b2title(" ") l1title("`varlist'") xtick(0)
    
    restore
    
    end
    Click image for larger version

Name:	turnip.png
Views:	1
Size:	18.7 KB
ID:	1472972



    But this is Stata 6 (==7 != 8) graphics. I like these kinds of displays too, but there is no need to rewrite the code to include Stata 8+ graphics, as

    Code:
    set scheme s1color
    dotplot mpg, center xla(none)
    gets you this, which would, I guess, generally be considered equivalent and even better and is any case much more flexible. Choose your own scheme and marker options, for example.
    Click image for larger version

Name:	turnip2.png
Views:	1
Size:	22.1 KB
ID:	1472973




    Indeed stripplot (SSC) is more flexible yet.

    Comment


    • #3
      This is very helpful, thank you. The dot plot works perfectly.

      Comment


      • #4
        Googling would have shown that I said the same in 2004: https://www.stata.com/statalist/arch.../msg00227.html

        Comment

        Working...
        X