Announcement

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

  • Setting Initial Parameters

    Set initial parameters

    Good morning
    I open this thread to ask if in Stata it is possible to create initial parameters (to change them according to requests).
    To explain myself better, I give a practical example.
    This is my initial situation:

    Code:
    forvalues i = 1/7 {
    gen CAPINV`=2012+`i'' = 0
    foreach j of numlist 214(7)269 {
    replace CAPINV`=2012+`i'' = CAPINV`=2012+`i'' + v`=`j'+`i''
    }
    }
    
    forvalues i = 1/7 {
    gen D`=2012+`i'' = 0
    foreach j of numlist 248(7)269 {
    replace D`=2012+`i'' = D`=2012+`i'' + v`=`j'+`i''
    }
    }
    Come vedete il numero 7 appare quattro volte (nei due forvalues e nei due foreach).
    Vorrei creare un parametro (ad esempio x=7) che mi permettesse di cambiare quel determinato numero solo una volta. Ad esempio:

    x=7
    Code:
    forvalues i = 1/x {
    gen CAPINV`=2012+`i'' = 0
    foreach j of numlist 214(x)269 {
    replace CAPINV`=2012+`i'' = CAPINV`=2012+`i'' + v`=`j'+`i''
    }
    }
    
    forvalues i = 1/x {
    gen D`=2012+`i'' = 0
    foreach j of numlist 248(x)269 {
    replace D`=2012+`i'' = D`=2012+`i'' + v`=`j'+`i''
    }
    }
    or
    x = 8
    Code:
    forvalues i = 1/x {
    gen CAPINV`=2012+`i'' = 0
    foreach j of numlist 214(x)269 {
    replace CAPINV`=2012+`i'' = CAPINV`=2012+`i'' + v`=`j'+`i''
    }
    }
    
    forvalues i = 1/x {
    gen D`=2012+`i'' = 0
    foreach j of numlist 248(x)269 {
    replace D`=2012+`i'' = D`=2012+`i'' + v`=`j'+`i''
    }
    }
    So directly change the number only in the paramtero without having to change it in all the formulas (here I have reported only two formulas, but I have several as well). is this possible in Stata (similar to Matlab)? Thanks!
    Last edited by Riccardo Busin; 04 Nov 2021, 18:40.

  • #2
    Like this:
    Code:
    local x = 7
    forvalues i = 1/`x' {
        gen CAPINV`=2012+`i'' = 0
        foreach j of numlist 214(`x')269 {
            replace ... /etc.
       }
    }
    
    forvalues i = 1/`x' {
        gen D`=2012+`i'' = 0
        foreach j of numlist 248(`x')269 {
            replace D`=2012+`i'' = ... // etc.
        }
    }
    will give you the code with x = 7. If you want to change it to 8, just change that first command to -local x = 8- and the change will be carried forward in the rest of the code.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      Like this:
      Code:
      local x = 7
      forvalues i = 1/`x' {
      gen CAPINV`=2012+`i'' = 0
      foreach j of numlist 214(`x')269 {
      replace ... /etc.
      }
      }
      
      forvalues i = 1/`x' {
      gen D`=2012+`i'' = 0
      foreach j of numlist 248(`x')269 {
      replace D`=2012+`i'' = ... // etc.
      }
      }
      will give you the code with x = 7. If you want to change it to 8, just change that first command to -local x = 8- and the change will be carried forward in the rest of the code.
      I thank you once again. Your help has been truly invaluable. These tips will be very helpful in writing the code required for my university research. Thanks again!

      Comment

      Working...
      X