Announcement

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

  • How do I replace the contents of one variable with that of another?

    In essence I want to tell STATA that when variable F11 = "Ya" replace it with the value in variable Score (SCREENSHOTS attached). [1]: https://i.stack.imgur.com/UvfDl.png

    So basically I want to replace the contents of the variable "F11" as well as all the other indicator numbers (A01, B02, C03 etc.) with the score that applies to that indicator.
    So for example, for the first observation in the screenshot, that person received a score of 19.88 (variable is Score) for the indicator F-11 (variable is Kat_Indikator_KG) and the label "Ya" under the varibale F-11 tells us that that individual was scored for this category.
    What I would like to do is replace the "Ya" with the score obtained in variable "Score" and I would like to do that for all the indictor variables e.g. (A01, B02, C03 etc.)

    So far I've tried the following, but none seem to work:

    replace F11 = Score if Kat_Indikator_KG == "F-11"
    replace F11 = Score if Kat_Indikator_KG == "F-11"
    replace F11 = Score if Kat_Indikator_KG == "F-11" & F11 == "Ya"
    replace F11 = Score if F11 == "Ya"enter code here

    Screenshots are attached and help is appreciated!


    [1]: https://i.stack.imgur.com/UvfDl.png
    Attached Files

  • #2
    You cannot replace a string variable with a numeric value. You could replace "Ya" with "1" and destring it, so something like this:
    Code:
    ds Kat Score, not
    foreach var of varlist `r(varlist)' {
        replace `var' = "1" if `var' == "Ya"
        destring `var', replace
        replace `var' == Score if `var' == 1
    }
    Edit: Cross-posted on SO here
    Last edited by Wouter Wakker; 09 Jun 2020, 06:21.

    Comment


    • #3
      Thanks Wouter! Dank je wel!
      With a few edits your method worked! Saved me a lot of time too!

      Comment

      Working...
      X