Announcement

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

  • Multiplying observations in a variable list

    In a dataset of multiple variable lists, I would like to multiply the value of only selected variables in one variable list by 4. Namely, I want to keep the observations unchaged if the observation concerns the age_group of <1 years and 95 plus (age_group variable list is not numeric). For all the other cases, I am trying to multiply the observation values by 4. I initially used this command: gen new_var = old_var * 4 if age_group != "<1 year" but I cannot incorporate 95 plus age group in this command. Also, in a new variable list, observations for <1 age group are deleted, while I would just like to keep the previous value. Thanks!

  • #2
    Originally posted by Mari Verulashvili View Post
    . . . I cannot incorporate 95 plus age group in this command. Also, in a new variable list, observations for <1 age group are deleted, while I would just like to keep the previous value.
    Then first create the new variables as duplicates of the source variables so that they begin with the same values, and then multiply by four when the age group variable's values match either of the two criteria. Something like the following.
    Code:
    foreach var of varlist <your selected variables> {
        clonevar new_`var' = `var'
        replace new_`var' = new_`var' * 4 if inlist(age_group, "<1 year", "95 plus")
    }

    Comment


    • #3
      As an addendum, you can do something like this, too
      Code:
      foreach var of varlist <your selected variables> {
          generate double new_`var' = cond(inlist(age_group, "<1 year", "95 plus"), `var' * 4, `var')
      }
      but I recommend the two-step approach in #2 above, instead. It's clearer and -clonevar- maintains variable attributes (especially, storage type) better.

      Comment


      • #4
        And if what Joseph explained above is not solving your problem, provide a sample dataset using -dataex- and clarify with respect to this set what you want to do.

        Comment


        • #5
          Originally posted by Joseph Coveney View Post
          Then first create the new variables as duplicates of the source variables so that they begin with the same values, and then multiply by four when the age group variable's values match either of the two criteria. Something like the following.
          Code:
          foreach var of varlist <your selected variables> {
          clonevar new_`var' = `var'
          replace new_`var' = new_`var' * 4 if inlist(age_group, "<1 year", "95 plus")
          }
          Thanks! I think this is doing the opposite of what I am trying, however. When running the code, it multiplies the values by 4 for when age group is <1 year and 95 plus. I am trying to maintain values for these age groups only and change ther rest (1 to 4, 5 to 9...e tc)

          Comment


          • #6
            Originally posted by Mari Verulashvili View Post
            I think this is doing the opposite of what I am trying, however. When running the code, it multiplies the values by 4 for when age group is <1 year and 95 plus. I am trying to maintain values for these age groups only and change ther rest (1 to 4, 5 to 9...e tc)
            Whoops, sorry. Flip the Boolean condition like this.
            Code:
            replace new_`var' = new_`var' * 4 if !inlist(age_group, "<1 year", "95 plus")

            Comment


            • #7
              Originally posted by Joseph Coveney View Post
              Whoops, sorry. Flip the Boolean condition like this.
              Code:
              replace new_`var' = new_`var' * 4 if !inlist(age_group, "<1 year", "95 plus")
              Thanks so much!

              Comment

              Working...
              X