Announcement

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

  • Creating a loop to check for data points

    Hi: I'm new to creating loops in Stata and hoping that someone can help. I want to create a new variable with yes/no, where it is yes for that patient if one of the other 7 variables in the dataset contains the term "CBC" for that patient. Thanks!

  • #2
    You don't need to do any looping to accomplish this. You use a technique called "vectorization" which allows you to preform operations on an entire vector at once. This technique is common to all statistical programing languages. If you provide a data example using the -dataex- command and post it here I will show you what you need to do. Don't worry, -dataex- is very easy to use.

    Comment


    • #3

      Code:
      gen wanted = inlist("CBC", var1, var2, var3, var4, var5, var6, var7)
      or some variation depending on what "contains" means precisely.

      Comment


      • #4
        Okay, so this is what my data looks like:

        input str14 newv1 str28(newv2 newv3 newv4 newv5)
        "BUN" "CBC" "" "" ""
        "BUN" "TAK" "CBC" "" ""
        "BUN" "TAK" "" "" ""
        "BUN" "TAK" "" "" "CBC"
        "CBC" "TAK" "" "" ""

        Comment


        • #5
          Code:
          clear 
          input str14 newv1 str28(newv2 newv3 newv4 newv5)
          "BUN" "CBC" "" "" ""
          "BUN" "TAK" "CBC" "" ""
          "BUN" "TAK" "" "" ""
          "BUN" "TAK" "" "" "CBC"
          "CBC" "TAK" "" "" ""
          end 
          
          gen wanted = inlist("CBC", newv1, newv2, newv3, newv4, newv5)
          
          list wanted new* 
          
               +------------------------------------------------+
               | wanted   newv1   newv2   newv3   newv4   newv5 |
               |------------------------------------------------|
            1. |      1     BUN     CBC                         |
            2. |      1     BUN     TAK     CBC                 |
            3. |      0     BUN     TAK                         |
            4. |      1     BUN     TAK                     CBC |
            5. |      1     CBC     TAK                         |
               +------------------------------------------------+

          Comment


          • #6
            Thanks Van, looks like you may have made this example data by hand? That's definitely doing things the hard way. Regardless, Nick's code should be sufficient to answer your question. One could edit it to match your example data like so:

            Code:
            gen wanted = inlist("CBC", newv1, newv2, newv3, newv4, newv5)

            Comment


            • #7
              Thank you to both of you! STATA came back saying "inlist not found"

              Comment


              • #8
                Oh nevermind, got it! Thanks again!

                Comment


                • #9
                  The code you used would show the reason for your error message, which would help other people. My guess is that first you left a space after inlist.

                  Comment

                  Working...
                  X