Announcement

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

  • Foreach loop - egen function

    At present, I need to generate a new variable for each value in the variable called "setting" (Int 1-12), which would be the sum of those values if they also meet a condition of the variable "core"

    The long form is below:

    egen int Ava_core_Home = sum(count) if setting =="Home" & core == 1, by (P_num)
    egen int Ava_core_Church = sum(count) if setting =="Church/church hall" & core == 1, by (P_num)
    egen int Ava_core_Fale koloa = sum(count) if setting =="Church/church hall" & core == 1, by (P_num)

    I tried something like:

    forvalues i = 1/`i(setting)' {
    egen int Ava_core_`i' =sum(count) if setting ==`i' & core == 1, by (P_num)
    }

    but keep getting syntax errors

    Thank you for your help
    Last edited by Tim Chambers NZ; 18 Jun 2018, 16:22.

  • #2
    i(setting) is something of your own invention.

    You don't need a loop here.

    Code:
    egen Ava_core = total(count), by(P_num setting core)
    holds all the totals. If you really need separate variables, reach for separate afterwards.

    Ava_core_Fale koloa would be an illegal variable name, because spaces aren't allowed.

    Comment


    • #3
      Welcome to Statalist.

      Please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post, looking especially at sections 9-12 on how to best pose your question. In particular, please read FAQ #12 and help those whose help you seek by posting example data using the dataex command. If you are running Stata 15.1 or later, it is already installed. For earlier versions of Stata, install dataex by typing ssc install dataex. Type help dataex to read the simple instructions for using it. Using dataex will enable those who want to help you to quickly and easily create a 100% faithful replica of your situation to test their ideas and code on.

      Beyond that, Section 12.1 is particularly pertinent

      12.1 What to say about your commands and your problem

      Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!
      ...
      Never say just that something "doesn't work" or "didn't work", but explain precisely in what sense you didn't get what you wanted.
      as is Section 12.3 on the use of code delimiters [CODE] and [/CODE] to present code and output copied and pasted from the Do-file Editor or the Results window.

      I will point out that your long form code
      Code:
      egen int Ava_core_Home = sum(count) if setting =="Home" & core == 1, by (P_num)
      egen int Ava_core_Church = sum(count) if setting =="Church/church hall" & core == 1, by (P_num)
      egen int Ava_core_Fale koloa = sum(count) if setting =="Church/church hall" & core == 1, by (P_num)
      seems unlikely to have actually worked correctly, since the third command is identical to the second command from the equal sign onward, and on the left of the equal sign, the "koloa" following the variable name is syntactically incorrect.

      An aphorism occasionally seen here is "if you can't tell us what you did, we can't tell you what you did wrong".

      The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

      Comment

      Working...
      X