Announcement

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

  • Local macro in "forvalues" function

    My first post here, but have used this forum extensively in the past to trouble shoot. However, can't find a solution to my stata problem, if there is one.

    I have a series of variables from DX1, DX2, DX3 ... DX25.

    Variables DX1 - DX25 have a diagnostic code, which is a string value.

    I'm trying to recreate a new variable that searches DX1-25, and pulls the diagnostic codes I am interested in.

    Write now, I have this code:

    gen AKI=0
    forvalues j=1/25{
    replace AKI=1 if inlist(DX`j', "5845", "5846", "5847", "5848", "5849")
    }

    But the problem is, for the inlist function, I can't include more than 5-10 string variables in the "inlist" function. For some new variables I am trying to generate, it will be based on 100s of diagnostic codes. So I thought if I use a macro with my string variables predefined, then I could avoid the 5-10 limit.

    I've tried creating the following macro and incorporating it into my loop:

    local aki "5845" "5846" "5847" "5848" "5849"

    gen AKI=0

    forvalues j=1/25{
    replace AKI=1 if inlist(DX`j', "`x'")
    }

    And it doesn't work. The result is "0 real changes made"

    Any help you might have would be tremendously appreciated. Thank you.

  • #2
    The below code worked fine. Perhaps recheck you initial try for typos or little errors.

    Code:
    clear
    set obs 50
    gen counter = _n
    gen DX1=""
    replace DX1 = "5845" if counter==45
    gen DX2=""
    replace DX2 = "5848" if counter ==3
    gen DX3 = ""
    replace DX3 ="5849" if counter == 27
    
    
    gen AKI=0
    forvalues j=1/3{
    replace AKI=1 if inlist(DX`j', "5845", "5846", "5847", "5848", "5849")
    }
    Last edited by Susan Wilson; 10 Feb 2020, 20:16.

    Comment


    • #3
      Sorry! I just reread why you're interested in using a local variable. I'll give it a another look.

      Comment


      • #4
        Thank you for your response. They are coded as string variables.

        My question was how to create a macro with my observations of interest (i.e. diagnostic codes) for variables DX1 - DX25, and to use that macro in my "inlist" function, so I can avoid the 5-10 string variable limit.

        I had thought of doing:


        local x "5845" "5846" "5847" "5848" "5849"

        gen AKI=0

        forvalues j=1/25{
        replace AKI=1 if inlist(DX`j', "`x'")
        }


        But the above produces "0 observations".

        Thank you.

        Comment


        • #5
          There are two problems: 1, using a local variable won't work with 'inlist', because it requires commas. 2, the local variable needs to be inside the loop.

          What about something like this, which just uses a nested loop. Although it might be a bit slow in large numbers.

          Code:
          clear
          set obs 50
          gen counter = _n
          gen DX1=""
          replace DX1 = "5845" if counter==45
          gen DX2=""
          replace DX2 = "5848" if counter ==3
          gen DX3 = ""
          replace DX3 ="5849" if counter == 27
          
          gen AKI=0
          forvalues j=1/3{
          forvalues k = 5845/5849 {
          replace AKI=1 if DX`j'=="`k'"
          }
          }

          Comment


          • #6
            Thank you so much for your response.

            But what if k is for a string variable? As in, 5845 - 5849 are stored as string variables. I was interested in knowing how to apply the above with a string value.

            Comment


            • #7
              From post #1
              So I thought if I use a macro with my string variables predefined, then I could avoid the 5-10 limit
              That is the source of your misunderstanding. Stata will first expand all the macros in a command with their values, and then count the number of arguments after all macros have been replaced by their expansions. So using a local macro does not avoid the problem.

              Comment

              Working...
              X