Announcement

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

  • Confusion regarding Loop Coding

    I'm trying to write a loop that will remove all observations except those from a specific data point.

    The code I've written:

    Code:
    foreach i of var _all {
         replace `i' = . if Location != 19
         }
    What stata gives me is:
    Code:
    type mismatch
    r(109)
    Is this because my Location variable is a part of var _all?

    If so, how do I write a loop that will remove all observations except those that belong to a specific type of observation designated by Location number 19?

  • #2
    Is this because my Location variable is a part of var _all?
    No, that's not the problem. Because you do not show example data, I cannot say exactly what is causing the problem. But there are two general, closely related, possibilities:

    1. Variable Location is a string variable, rather than numeric. In that case -if Location != 19- is the cause of the mismatch and has to be changed to -if Location != "19"-.
    2. At least one of the variables other than Location in your data set is a string variable. When the loop reaches that variable, -replace `i' = .- is the cause of the type mismatch. This is a bit more complicated to code around as you have to branch on the storage type of the variable `i' and -replace `i' = ""- for the string variables, while still using -replace `i' = .- for the numeric ones.

    Note that both of these possibilities may be going on in your actual data. Or may only one of them.

    All of that said, I don't get the point of replacing the value of every variable in the observation with missing value. Why not just get rid of the observation altogether ? It also makes the code much simpler:
    Code:
    drop if Location != 19 // OR -drop if Location != "19" IF LOCATION IS A STRING
    In the future, when asking for help with code, always show example data. When showing example data, always use -dataex-. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      You were right!

      My location variable WAS a string variable. But I was also using the wrong variable in my command, as I had created a clone variable and replaced the strings with numerical categories.

      The moment I changed my command to the correct variable, it worked.

      In the future, I will include -dataex-. Sorry for the miscommunication.

      Thanks!

      Comment

      Working...
      X