Announcement

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

  • Replacing an observation

    Dear colleagues ,
    I forgot to write in my last post that I wanted to replace the first observation of s1 with spn_serotype if s1 is 1 but combining with Q. I dont want to do this directly like replace s1="Q1" if spn_serotype=="13" since I have several s in my full data i.e s1 to s67 whose value 1 correspond to different spn_serotypes and so I want a code which I can generalize using foreach loop easily. It is not working for me.see my code and data below. Kindly assist




    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str11(spn_serotype s1)
    "13"  "0"
    "15B" "0"
    "35B" "0"
    "8"   "0"
    "35B" "0"
    "19F" "0"
    "21"  "0"
    "29"  "0"
    "1"   "1"
    "1"   "1"
    "1"   "1"
    "1"   "1"
    "1"   "1"
    "1"   "1"
    "1"   "1"
    end
    ------------------ copy up to and including the previous line ------------------

    Listed 15 out of 15 observations

    . replace s1="Q"+spn_serotype if s1=="1" in 1
    (0 real changes made)
    Regards,
    Fred Orwa

  • #2
    Code:
    replace s1="Q"+spn_serotype if s1=="1" & s1[_n-1]=="0"
    It's only for the example data, unless the "first observation of s1 if s1 == 1" is well-defined in your whole sample.

    Comment


    • #3
      Thanks Fei

      Comment

      Working...
      X