Announcement

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

  • forvalues loop with the use of local values

    I got stuck in using forvalues with local values. I have variable x as an integer 1-100. I would like to separate my file based on x. So I summarize x to ret r(min) and r(max) and assign local names.

    Here is my example.

    Code:
    use "${UPDATE2020}abcd-wip01.dta", clear
    summ x, detail
    local low=r(min)
    local hi=r(max)
    
    forvalues i=`low'/`hi' {
    use "${UPDATE2020}abcd-wip01.dta", clear
    di "keep `i'"
    keep if x==`i'
    saveold "${UPDATE2020}tariff-wip01-keep`i'.dta", replace
    }
    However, I get an error from Stata 16.1 as

    invalid syntax
    r(198);

    I have no clues why giving the name for r(min)/r(max) gives an error to the code. I used the code above for Stata 13.1 a year ago and it worked fine. Kindly let me know if there is any change in Stata Syntax I should know.

    Strangely, when I use r(min)/r(max) directly to this code, it works fine for me.

    Code:
    use "${UPDATE2020}abcd-wip01.dta", clear
    summ x, detail
    
    forvalues i=`r(min)'/`r(max)' {
    use "${UPDATE2020}abcd-wip01.dta", clear
    di "keep `i'"
    keep if x==`i'
    saveold "${UPDATE2020}abcd-wip01-keep`i'.dta", replace
    }
    Are there any ways I can assign the new names to the stored values as local values for Stata 16.1? Kindly let me know.

    Best,
    Chorthip
    Last edited by Chorthip Utoktham; 16 Jun 2021, 06:01.

  • #2
    Try change that -display- command into:

    Code:
    display "keep " `i'

    Comment


    • #3
      Thank you very much. It works. And with your technique, I also try

      Code:
       
       display " keep `i' "
      and it works too. I cannot figure out why it works but it does work.

      Thank you for a great tip.

      Comment

      Working...
      X