Announcement

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

  • scalar or local name in loops

    Hello! I was not able to perform the following code and error message says that _"sm35_temp invalid name
    when I use either local or scalar. Thank you for your help!

    set more off
    logit OwnLivestock2 m35_temp c.m35_temp#c.m35_temp m610_temp c.m610_temp#c.m610_temp m112_temp c.m112_temp#c.m112_temp ///
    m35_prec c.m35_prec#c.m35_prec m610_prec c.m610_prec#c.m610_prec m112_prec c.m112_prec#c.m112_prec ///
    i.fine_soil Dist_River Dist_Port Dist_Market, robust

    local climate m35_temp m610_temp m112_temp m35_prec m610_prec m112_prec fine_soil Dist_River Dist_Port Dist_Market
    local country Bangladesh Indonesia SriLanka Thailand VietNam
    foreach co of local country {
    foreach c of local climate {
    qui sum `c' if country == "`co'"
    local "s`c'" = r(mean)
    local "s`c'2" = r(mean)^2
    }
    lincom sm35_temp*m35_temp + sm610_temp*m610_temp + sm112_temp*m112_temp + sm35_prec*m35_prec + sm610_prec*m610_prec + sm112_prec*m112_prec ///
    + sfine_soil*i.fine_soil + sDist_River*Dist_River + sDist_Port*Dist_Port + sDist_Market*Dist_Market
    outreg2 using lincom.xls, nolabel excel dec(5) rdec(3) aster(coef) append
    }
    *

  • #2
    Instead of

    Code:
    local "s`c'" = r(mean) 
    local "s`c'2" = r(mean)^2
    try

    Code:
    local s`c' = r(mean) 
    local s`c'2 = r(mean)^2

    Comment


    • #3
      Thank you for helping, but it still does not work.

      sm35_temp not found
      r(111);

      Comment


      • #4
        OK. But does sm35_temp exist as a variable or scalar when you try to use it? It seems that you have a local macro with that name, but that's quite different. If you want to refer to the local macro, then you need

        Code:
         
        `sm35_temp'

        Comment


        • #5
          Hi!

          so I try to modify code to see when will happen when temperature increase constantly by 2, however I receive an error message as below:
          Variable | Obs Mean Std. Dev. Min Max
          -------------+---------------------------------------------------------
          cm35_temp | 356 29.57279 .776987 27.63333 31.16667
          cm610_temp | 356 30.23238 .3783611 29.24 30.9
          cm112_temp | 356 22.74818 .7807253 21.05 24.575

          Variable | Obs Mean Std. Dev. Min Max
          -------------+---------------------------------------------------------
          cm35_prec | 356 12.50488 5.019344 4.666667 31.06667
          cm610_prec | 356 34.6594 11.22305 22.62 80.64
          cm112_prec | 356 1.55192 .5509248 .775 3.475

          Variable | Obs Mean Std. Dev. Min Max
          -------------+---------------------------------------------------------
          fine_soil | 356 .1713483 .3773434 0 1
          varlist not allowed
          r(101);

          the code I run is here:

          set more off
          drop cm35_temp cm610_temp cm112_temp cm35_prec cm610_prec cm112_prec
          gen cm35_temp = m35_temp +2
          gen cm610_temp = m610_temp +2
          gen cm112_temp = m112_temp +2

          gen cm35_prec = m35_prec
          gen cm610_prec = m610_prec
          gen cm112_prec = m112_prec

          local temp cm35_temp cm610_temp cm112_temp
          local prec cm35_prec cm610_prec cm112_prec
          local climate \`temp' \`prec' fine_soil Dist_River Dist_Port Dist_Market
          local country Bangladesh Indonesia SriLanka Thailand VietNam

          foreach co of local country {
          foreach c of local climate {

          sum `c' if country == "`co'"
          global s`c' == r(mean)
          global s`c'2 == r(mean)^2

          }
          logit OwnLivestock2 cm35_temp c.cm35_temp#c.cm35_temp cm610_temp c.cm610_temp#c.cm610_temp cm112_temp c.cm112_temp#c.cm112_temp ///
          cm35_prec c.cm35_prec#c.cm35_prec cm610_prec c.cm610_prec#c.cm610_prec cm112_prec c.cm112_prec#c.cm112_prec ///
          i.fine_soil Dist_River Dist_Port Dist_Market, robust

          lincom $scm35_temp * cm35_temp + $scm610_temp * cm610_temp + $scm112_temp * cm112_temp + $scm35_prec * cm35_prec + $scm610_prec * cm610_prec + $scm112_prec * cm112_prec + ///
          $scm35_temp2 * c.cm35_temp#c.cm35_temp + $scm610_temp2 * c.cm610_temp#c.cm610_temp + $scm112_temp2 * c.cm112_temp#c.cm112_temp + ///
          $scm35_prec2 * c.cm35_prec#c.cm35_prec + $scm610_prec2 * c.cm610_prec#c.cm610_prec + $scm112_prec2 * c.cm112_prec#c.cm112_prec ///
          + $sfine_soil * 1.fine_soil + $sDist_River * Dist_River + $sDist_Port * Dist_Port + $sDist_Market * Dist_Market

          outreg2 using lincom.xls, nolabel excel dec(5) rdec(3) aster(coef) append
          }
          *


          THANKS!

          Comment


          • #6
            Your macros and loops are generating expressions that are not suitable for the -global- macro definitions contained in those loops. To see what is going on, we can take out the -sum- and -global- commands and just -display- what the macros are doing through the loops:

            Code:
            . clear*
            
            . local temp cm35_temp cm610_temp cm112_temp 
            
            . local prec cm35_prec cm610_prec cm112_prec 
            
            . 
            . local climate \`temp' \`prec' fine_soil Dist_River Dist_Port Dist_Market
            
            . local country Bangladesh Indonesia SriLanka Thailand VietNam
            
            . 
            . foreach co of local country {
              2.         display `"COUNTRY: `co'"'
              3.         foreach c of local climate {
              4.                 display `"s`c'"'
              5.         }
              6. }
            COUNTRY: Bangladesh
            scm35_temp cm610_temp cm112_temp
            scm35_prec cm610_prec cm112_prec
            sfine_soil
            sDist_River
            sDist_Port
            sDist_Market
            COUNTRY: Indonesia
            scm35_temp cm610_temp cm112_temp
            scm35_prec cm610_prec cm112_prec
            sfine_soil
            sDist_River
            sDist_Port
            sDist_Market
            COUNTRY: SriLanka
            scm35_temp cm610_temp cm112_temp
            scm35_prec cm610_prec cm112_prec
            sfine_soil
            sDist_River
            sDist_Port
            sDist_Market
            COUNTRY: Thailand
            scm35_temp cm610_temp cm112_temp
            scm35_prec cm610_prec cm112_prec
            sfine_soil
            sDist_River
            sDist_Port
            sDist_Market
            COUNTRY: VietNam
            scm35_temp cm610_temp cm112_temp
            scm35_prec cm610_prec cm112_prec
            sfine_soil
            sDist_River
            sDist_Port
            sDist_Market
            In your actual loops, this means you are trying to execute commands such as:

            Code:
            global scm35_temp cm610_temp cm112_temp = `r(mean)'
            which, of course is a syntax error. Stata thinks that scm35_temp cm610_temp cm112_temp is a varlist, which is not allowed after global: you can only specify one name in a single -global- command.

            Let me also point out that after you fix that problem, there is almost certainly a logic flas. Notice that the `"s`c'"'s that you create are the same for each country. That means that when this finally runs, the same globals will be overwritten each time through the outer loop. Probably what you really intend is to create a new crop of global macros for each country: if so, the names of those globals have to mention `co'. [If you really intend to just re-evaluate the same global macros over and over, then it is a waste of time to do it in a loop. Since the end result is just to store the value associated with VietNam in those global macros, you might as well scrap the outerloop and replace -if country == "`c'"- with -if country == "VietNam"-.]

            I will, for now, spare you my usual rant about why you should use local rather than global macros, because at the moment you have bigger fish to fry here.

            Comment

            Working...
            X