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

  • Loop to drop specific observations

    I'm trying to create a loop that automatically replaces all observations with a "." with exception to the observations associated with a specific Location.

    I need some help understanding the anatomy of the syntax because the one I'm trying is not working.

    I'm trying:

    HTML Code:
    foreach v in `r(varlist)' {replace `v' = . if Location !== 19}
    I'm copying this command from a previous destring loop that I was briefly taught about, so there could be some naiive misunderstanding that's making it non-functional.

    I understand that
    HTML Code:
    refers to literally "for each"

    However, the letter v, is confusing. Does stata understand "v" to mean "variable"?

    Also, for
    HTML Code:
    , my understanding is that the compound quotations are for strings. Are they necessary here?

    What would the correct code be?


  • #2
    I've now worked out the following:

    HTML Code:
    foreach i of local varlist _all {
    replace `i' = . if Location !==19
    Stata then returns an error that says:

    HTML Code:
    { required


    • #3
      This question is nothing to do with Mata. That sounds picky, I guess, but you would have got a much quicker reply posting in General.

       foreach i of local varlist _all {
      would be better as

      foreach i of var _all {

      != not !== is the not equals operator. That is certainly one reason why the code in #1 would not work.

      There are various tutorials on loops here and there. One that is familiar to me is