Announcement

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

  • Changing variable value based on the value and name of another variable

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str12 Source float(sheep_goat cattle chicken virulence_combined)
    "chicken"    . . . 0
    "cattle"     . . . 1
    "cattle"     . . . 1
    "sheep_goat" . . . 1
    "sheep_goat" . . . 2
    "sheep_goat" . . . 0
    "sheep_goat" . . . 1
    "chicken"    . . . 0
    "sheep_goat" . . . 2
    "sheep_goat" . . . 1
    end
    label values sheep_goat virulencel
    label values cattle virulencel
    label values chicken virulencel

    I am trying to create a variable that categorizes the number of virulence genes for a particular source. For example, if the value of source is 'sheep_goat' as in line 5, I would like the sheep_goat variable to be 1 based on the variable virulence_combined. The sheep_goat variable will be missing when the source is not sheep_goat. I am having a hard time thinking about how to tell Stata to change the value of one variable (sheep_goat) based on the value of another variable (virulence_combined) if the value of another variable (Source = "sheep_goat") is the same as the name as that variable (sheep_goat). I hope this makes sense.

  • #2
    I am confused by your example
    For example, if the value of source is 'sheep_goat' as in line 5, I would like the sheep_goat variable to be 1 based on the variable virulence_combined.
    because the actual value of virulence_combined in that line of your input is 2, not 1. I'll assume you made a typo and just follow the rest of the explanation.
    Code:
    foreach v of varlist sheep_goat cattle chicken {
        replace `v' = cond(Source == "`v'", virulence_combined, .)
    }
    That said, why are you doing this? You will be creating a data layout that is going to be very difficult to use for further data management or analysis. The data, instead of being compactly represented in just the two variables Source and virulence_combined, is going to be spread out over multiple variables. This takes up more space, most of which is wasted on missing values, and it is poorly matched to the requirements of most Stata commands. So where are you going with this? You will probably be better off not doing this, as there will likely be a better way to do whatever you have in mind without creating this odd layout.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      I am confused by your example

      because the actual value of virulence_combined in that line of your input is 2, not 1. I'll assume you made a typo and just follow the rest of the explanation.
      Code:
      foreach v of varlist sheep_goat cattle chicken {
      replace `v' = cond(Source == "`v'", virulence_combined, .)
      }
      That said, why are you doing this? You will be creating a data layout that is going to be very difficult to use for further data management or analysis. The data, instead of being compactly represented in just the two variables Source and virulence_combined, is going to be spread out over multiple variables. This takes up more space, most of which is wasted on missing values, and it is poorly matched to the requirements of most Stata commands. So where are you going with this? You will probably be better off not doing this, as there will likely be a better way to do whatever you have in mind without creating this odd layout.
      Thanks so much Clyde.
      I am doing this largely to perform some basic description of the data and create some descriptive tables. I am not changing the data frame otherwise.

      Comment

      Working...
      X