Announcement

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

  • Using -foreach- to count for string variables

    Dear Statalists,

    I have a dataset with 28 string variables named"ox*tc", and the * represent to number 02, 03, ..., 31. They are all string variables with values ranged from letter "A" to "T". Now I'd like to creat a new variable, counting the total number of "T" or "A" in these variables. I tried foreach loop but it doesn't work, with some issues about the abbreviation.

    Code:
    foreach v of varlist ox*tc {
      count if ox*tc=="A" | ox*tc=="T"
    }
    I also tried this one but it still doesn't work.

    Code:
    forvalues i in 2/31{
    foreach v of varlist ox`i'tc {
      count if ox`i'tc=="A" | ox`i'tc=="T"
    }
    }
    I hope to have some suggestions about my mistakes and solutions, thank you very much!

  • #2
    Your first code is almost right.
    Code:
    foreach v of varlist ox*tc {
        count if `v' == "A" | `v' == "T"
    }
    In the future, when you want help troubleshooting code, don't just say that the code "didn't work." Describe exactly what happened: did Stata crash? did Stata hang? Did you get an error message--and, if so, what was that error message? Did it run without error messages but give incorrect results? If so, what were the incorrect results, and unless it's obvious, how do they differ from what you wanted and expected?

    In this case, the problem was easy to spot. But in general, posting code and saying no more than that it didn't work will not enable people to help you out. More information than that is usually needed.

    Comment


    • #3
      I agree strongly with the general and specific advice of Clyde Schechter. If the intent really is a new variable, then you need something like

      Code:
      gen count = 0  
      foreach v of varlist ox*tc {  
          replace count = count + inlist(`v', "A", "T")  
      }
      Last edited by Nick Cox; 08 Feb 2020, 12:13.

      Comment


      • #4
        Originally posted by Clyde Schechter View Post
        Your first code is almost right.
        Code:
        foreach v of varlist ox*tc {
        count if `v' == "A" | `v' == "T"
        }
        In the future, when you want help troubleshooting code, don't just say that the code "didn't work." Describe exactly what happened: did Stata crash? did Stata hang? Did you get an error message--and, if so, what was that error message? Did it run without error messages but give incorrect results? If so, what were the incorrect results, and unless it's obvious, how do they differ from what you wanted and expected?

        In this case, the problem was easy to spot. But in general, posting code and saying no more than that it didn't work will not enable people to help you out. More information than that is usually needed.
        Thank you Clyde! Sorry I missed the returning information of stata. For the first code, the problem is "ox ambiguous abbreviation". For the second code, the problem is "invalid syntax". Thank you for your reply, it's working!

        Comment


        • #5
          Originally posted by Nick Cox View Post
          I agree strongly with the general and specific advice of Clyde Schechter. If the intent really is a new variable, then you need something like

          Code:
          gen count = 0
          foreach v of varlist ox*tc {
          replace count = count + inlist(`v', "A", "T")
          }
          Thank you Nick! I'm struggling with the loop issue before, and yes, I need to creat a new variable for the result of count. Thank you for the help!

          Comment

          Working...
          X