Announcement

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

  • Using forvalues double loop gives "invalid name" r198

    Hi all, I'm trying to run a loop using forvalues as in the following:
    Code:
    forvalues `i'=0/1{
    forvalues `j'=0/9{
    capture noisily use "$temp\4_20`i'`j'_revised.dta"
    capture noisily replace mandal_name="Am" if mandal_name=="Am Muncipality"
    capture noisily replace mandal_name="An" if mandal_name=="An Muncipality"
    capture noisily replace mandal_name="El" if mandal_name=="El Municipality"
    capture noisily replace mandal_name="Sr" if mandal_name=="Sr Muncipality"
    capture noisily replace mandal_name="De" if mandal_name=="Dev"
    
    save "$temp\4_20`i'`j'_revised.dta",replace
    
    clear
    
    }
    }
    .
    I define the
    Code:
    $temp
    global before running the loop.

    I get the following error
    Code:
    _= invalid name
    r(198);
    I'm not sure where the syntax has gone wrong. The file names are exactly as indicated in the loop: the actual file names are "4_2007_revised", "4_2018_revised" etc.

    Would appreciate any help in this regard.

    Thanks

  • #2
    The typical syntax for forvalues is
    Code:
    forvalues i = 0/1 {
        forvalues j = 0/9 {
    and not
    Code:
    forvalues `i'=0/1{
    forvalues `j'=0/9{

    Comment


    • #3
      To cycle over 2000/2019, it is easier to go

      Code:
      forval y = 2000/2019 { 
              
           capture noisily use "$temp\4_`y'_revised.dta"      
      
      } 

      Comment


      • #4
        Originally posted by Joseph Coveney View Post
        The typical syntax for forvalues is
        Code:
        forvalues i = 0/1 {
        forvalues j = 0/9 {
        and not
        Code:
        forvalues `i'=0/1{
        forvalues `j'=0/9{
        Thanks Joseph!

        Comment


        • #5
          Originally posted by Nick Cox View Post
          To cycle over 2000/2019, it is easier to go

          Code:
          forval y = 2000/2019 {
          
           capture noisily use "$temp\4_`y'_revised.dta" 
          
          } 
          Thanks Nick for the advice

          Comment

          Working...
          X