Announcement

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

  • How do I use replace if function to go through several variables at once (i.e. variables 1 through 40)?

    Hi I am trying to sort through a large amount of data and narrow it down. To give a clear example, I have variables 1 through 40 with each variable representing a diagnosis.

    I am using the replace if function to narrow down the diagnoses I want to keep in my future analysis. So my do file looks something like this:

    replace had_bleeding=1 if diagnosis1=="nosebleeding"
    replace had_bleeding=1 if diagnosis2=="nosebleeding"

    and so forth...

    My question is if there is a way to run the code so I can go through variables 1 through 40 quickly, like:

    replace had_bleeding=1 if diagnosis1 through diagnosis40=="nosebleeding"

    Is there a good way to do this in STATA? Otherwise, I have to write each one independently which is time-consuming.

    Would appreciate the help, thanks.

  • #2
    Code:
    forval i= 1/40{
       replace had_bleeding=1 if diagnosis`i'=="nosebleeding"
    }

    Comment


    • #3
      Although I personally would do as Andrew Musau suggests, note that for persons not loop-experienced, -egen- provides some alternatives, the shortest being:
      Code:
      egen had_bleeding = anymatch(diagnosis*) values(1)

      Comment


      • #4
        Mike Lacy Unfortunately anymatch() is restricted to looking for specified integer value(s) and the problem here concerns strings. Naturally, if the OP encoded their variables, that problem would go away.

        Comment


        • #5
          Whoops, thanks for catching my mistake. I had noticed that it was a string, got distracted by something else, and forgot before I posted <grin>.

          Comment


          • #6
            Been there, done that.

            Comment


            • #7
              Thanks for the replies and help everyone.

              Andrew Musau I ended up using a foreach var loop that works perfectly , it looks like this as an example (with K920 being a code designated for vomiting blood & using the actual data names I10_DX1 that stand for diagnosis 1, etc) :

              foreach var of varlist I10_DX1-I10_DX40 {
              replace had_Ugibleed=1 if `var'=="K920"
              }


              Now I have one more thing I am running into problems with which is I want to select for patients that have 2 diagnosis codes designated for bleeding (not just one)on their diagnoses list of 1-40. I tried to do it this way but I got the error message 'type mismatch' on STATA:

              foreach var of varlist I10_DX1-I10_DX40 {
              replace had_Ugibleed=1 if `var'=="K922" & `var'="K2210"
              }


              Anyone have tips on how to best go about this in STATA? Thanks again

              Comment


              • #8
                Two issues

                #1
                `var'="K2210"
                You need a double equal sign.

                #2

                `var'=="K922" & `var'=="K2210"
                Both these conditions cannot hold at the same time. So I think you want


                Code:
                gen tag1=0
                gen tag2=0
                foreach var of varlist I10_DX1-I10_DX40{
                   replace tag1 =1 if `var'=="K922"
                   replace tag2=1 if `var'=="K2210"
                }
                gen wanted= tag1&tag2

                Comment


                • #9
                  Andrew Musau You are an All-star my friend. Your suggestion worked exactly right.
                  I already had generated a 'wanted' tag so at the very end my code instead said something like this:

                  Code:
                  replace wanted=1 if tag1==1 & tag2==1
                  Thanks so much again, truly makes a big difference

                  Comment

                  Working...
                  X