Announcement

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

  • Absolute value

    I'm having trouble generating a variable using the absolute value of another variable. I want to indicate if population has changed (increased or decreased) by 5%. I'm able to generate an indicator for 5% increase but not decrease using the code below.

    First, I calculated 5% (per5) of the total end of year population (totend). Second, I generated an indicator variable if per5 is greater or equal to the difference in totals (diffb). I'm sure it is obvious that this only creates a variable indicating a 5% increase but I also want to indicate decrease. I've experimented with the absolute values function, but I had no luck.

    gen per5= .05*totend
    gen change5=1 if (diffb>=per5)

    Any suggestions? Thanks in advance for help.

  • #2
    Code:
    gen change5=abs(diffb)/totend > 0.05
    hth,
    Jeph

    Comment


    • #3
      Thanks Jeph! It worked.

      Comment


      • #4
        I have a follow-up question for computing with absolute values. I would like to calculate means and ranges of absolute values. Any suggestions on how to do this? thanks.

        Comment


        • #5
          Hmmm... given that you have a variable/variables with the absolute value, is there any reason you can't just use the -summarize- command on the new variable(s)? or am I missing something?

          Comment


          • #6
            Ben is right. Everything is easy once you create a new variable, but there's no other way.

            Comment


            • #7
              Okay. I see. thanks.

              Comment

              Working...
              X