Announcement

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

  • Bulk edit observations

    Hi guys,

    First time posting and still very new to the Stata so please bear with me. My problem will hopefully be very simple to resolve for those who are experienced with stata...

    Example data:

    trialno
    bwh-0331-mat
    bwh-0332-mat
    bwh-0342-mat
    bwh-0343-mat
    bwh-0344-mat
    bwh-0345-mat
    bwh-0346-mat
    bwh-0347-mat

    I would like to replace all the observation values so that "mat" is replaced with "pat".

    Instead of individually replacing the observations...there must be a clever way to do it with a couple of lines of command. Maybe using a loop of sort.

    Help please?


    Last edited by Yealin Chung; 20 Apr 2022, 05:02.

  • #2
    Code:
    replace trialno=subinstr(trialno, "mat", "pat",.)
    In the future, please present your data using the dataex command. See the Frequently Asked Questions section for more.

    EDIT: Per Nick's advice, this is why we ask for data examples in the manner i describe. I presumed, perhaps wrongly, that the variable is a normal string variable, but it's perfectly particle it's encoded with value labels, so if nothing else to avoid guesswork, ideally we'd see your data using dataex.

    Welcome to Statalist, Yealin.
    Last edited by Jared Greathouse; 20 Apr 2022, 05:25.

    Comment


    • #3
      Is trialno numeric with value labels or string?

      Code:
      describe trialno
      will tell you.

      Comment


      • #4
        In addition to Jared's points in #2 note that in principle -- so far as we know -- the substring "mat" could occur in other places than the last three characters.

        Comment


        • #5
          Nick: It is string.

          Jared: your code was what I was looking for.

          Noted your advice on posting etiquette for the future.

          Thank you both!

          Comment


          • #6
            In reply to Nick's second post

            Thankfully "mat" does not occur in other places - but yes, I can see how this could be in issue if I tried to adapt it.
            Cant even wrap my head around how I would deal with it...if it did, but useful to note for the future. thanks a lot

            Comment


            • #7
              The needed trickery depends on what else is allowed, but here is one idea among several:


              Code:
              replace trialno = subinstr(trialno, "-mat", "-pat", 1)

              Comment


              • #8
                Thanks Nick.

                Looking at your code, I can see how you have adapted it.

                May I ask, what does the last digit "1" at the end of the code indicate?

                In other words, what happens if I change it to "0" or "." or any other digit?


                Comment


                • #9
                  The subinstr() function is documented in the output of
                  Code:
                  help subinstr()
                  which explains what the fourth argument does, and gives examples.

                  Comment

                  Working...
                  X