Announcement

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

  • Stata does not see my local macro variables

    Hi,

    First of all, I tried the suggestions written here, but it didn't work for me. https://www.stata.com/support/faqs/p...-local-macros/

    Whenever I want to use local variables, stat does not see the lines and gives synthax error. I also tried using only the command line, but unfortunately the result did not change.

    I have 500 lines of code but it does not see the macro parts. Here is a section of the code I want to execute and the error I received.

    HTML Code:
    global origdatadir $origdaradir1
    global dodir $dodir1
    global dodirsup $dodirsup1
    global dodirsup2 $dodirsup2
    global dodirout $dodirout1
    global igmdodir $igmdodir1
    global outdatadir $outdatadir1
    global logdir $logdir1
    
    global graphdir $graphdir1
    global graphdirwmf $graphdirwmf1
    global graphdirpdf $graphdirpdf1 
    global texdir $texdir1 
    cd $rootdir
    
    set more off
    global countryselect $countryselect
    
    global yearselect $yearselect
    global yearcompare $yearcompare
    di "`countryselect'"
    global years 2006 2007 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019// TR
    global indepvars $indepvars
    local agemin $agemin
    local agemax $agemax
    local ageminlow = `agemin' - 1
    
    gen sex = fk090
    cap label var sex "Gender"
    cap label val sex fk090
    label define sex 1 "Male" 2 "Female"
    label val sex sex
    
    gen sex1 = sex == 1 
    label var sex1 "Male"
    gen sex2 = sex == 2
    label var sex2 "Female"
    
    gen age = fk070 // age used for health questionnaire
    gen age2 = age^2
    cap label var age "Age"
    
    
    if `agemax' < 80 {
    local agemaxhigh = `agemax' + 1
    local agemax80 "(`agemaxhigh'/max = .)"
    }
    When I ran this gave me the error below:

    Code:
    . if `agemax' < 80 {
    <80 invalid name
    r(198);
    
    end of do-file
    
    r(198);
    
    .
    There's a quite few local macro in my code so I really need to solve this problem. Any help wpuld be appriciated.

  • #2
    In your if-statement you specify that something is less than 80. However, that something is "nothing" (the error message tells you that: "<80" is invalid. You can inspect the contents of your local macro `agemax' by using the -display- command, for example
    Code:
    di `"|`agemax'|"'
    will show you between | and | the contents of your local macro.

    A general remark: You are using an awful lot of global macros with strange content (e.g. $grafdir1 -- if you do, why do you define another global macro named $grafdir instead of simply using $grafdir1 immediately?), this is a dangerous thing to do and has been discussed in a lot of places (e.g. see https://www.statalist.org/forums/for...ocal-vs-global). Additionally, you are using
    Code:
    local agemax $agemax
    without defining $agemax (at least not visible in the syntax snippet you are showing us) (plus: the local macro is superfluous here because you could use $agemax directly). I would advise to reconsider from the scratch what you are trying to achieve.

    Comment


    • #3
      In Stata variables are, in other terms, the columns or fields in a dataset.

      Local macros are, just that, local macros, and strictly not -- in Stata usage -- variables at all.

      That said, Dirk Enzmann gives excellent advice. It's evident that Stata can't see a definition for your local macro, and so asking to display it just beforehand will likely confirm that.

      There are two levels to explanation, one that you never defined the local macro at all, which is consistent with the code you show, and the other that you did define it somewhere but in a different namespace. That is what local means.

      Even if you have code in a do-file, or equivalently in a do-file editor window, it must be executed all (*) at once for the definitions of local macros to be visible for use in evaluating local macro references. Much more on this point can be found here in https://journals.sagepub.com/doi/abs...urnalCode=stja

      (*) "all" means minimally that the command defining a local macro and any command referring to it must both be within the same chunk of code.

      Details arising from the FAQ Advice. See https://www.statalist.org/forums/help #6 and #8.

      1. You cross-posted at https://stackoverflow.com/questions/...tnax-error-198 You are asked to tell us about cross-posting. It's also courteous on Stack Overflow.

      2. Max Planck was a great physicist; if that is your name too, then fine. Otherwise please note our longstanding request that people use full real names here.

      Comment


      • #4
        As a footnote to #3 I did notice the line

        Code:
         
         local agemax $agemax
        so why do I say that you didn't visibly define that macro? The answer lies in no definition of the global macro agemax being visible. If that is so, local agemax followed by nothing (the result of evaluating a global macro that doesn't exist) abolishes the local macro. if it exists already -- or does nothing otherwise.

        A local macro being assigned an empty string and a local macro being abolished (deleted, destroyed, removed) are one and the same in Stata.

        Comment

        Working...
        X