Announcement

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

  • Using wild card in variable name for loops

    Looking for some advice regarding replacing/recoding variable dependent on responses in other variables.

    The platform we use for data collection is ODK. For one question regarding side effects we have a 'select multiple' option on the database where the options are coded 0-13 (0=rash, 1=itchiniess, etc). When .csv data gets exported to stata the column 'sideeffects_initial' reads 0 2 4 5 10 or 1 11 12 for example which stata reads as string because of the spaces. Previously I'd been able to manage this problem with the code

    replace itchiness = regexm(sideeffects_initial, "1")

    but because this time I have more than 9 options this also recodes 10/11/12/13 as being positive for itchiness. I tried to search for help with this problem and in the end I've managed to split the variables into columns using:


    split sideeffects_initial, parse(" ")


    Then because I want to use wildcard of sideeffects_initial* I just renamed the initial variable


    rename sideeffects_initial_o sideeffects_ini_o


    I am then able to destring the newly created variables with

    destring sideeffects_initial*, replace

    I then want to create a new variable for each side effect (example itchiness, where it was originally coded 1) where if the number in any sideeffects_initial* column is equal to the side effect number. This is where I am having trouble.

    I have tried using

    egen itchinessss==1 if sideeffects_initial*==1

    Which I believe didn't work as egen can't be used with wildcards.

    I have then been trying to work out if it is possible to create a loop using wildcards.

    I know it is possible to type out each individual variable name in a loop and do this but trying to avoid this as we have this question across multiple datasets and so would have to code each individually based on the number of sideeffects_initial(1-13) that is generated depending on the data inputted.

    I came across the 'unab' function which I though may help - but still not working (error code: vars not found) based on below code.

    unab vars : sideeffects_initial*
    local varlist "vars"
    foreach x in `varlist' {
    replace itchiness=1 if `varlist'==1
    }


    Any advice greatly appreciated.

  • #2
    Code:
    foreach x of varlist sideeffects_initial* {
        replace itchiness=1 if `x'==1
    }
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Maarten shows a first principles solution, assuming that itchiness already exists. There's a convenience shortcut:

      Code:
       
       egen itchiness = anymatch(sideeffects_initial*), values(1)
      The reason that

      Code:
      egen itchiness ==1 if sideeffects_initial*==1
      doesn't work isn't as you diagnose.

      First, the first == is wrong as an operator. You need =.

      Second, egen needs a function call, as above.

      Third, the problem with that wildcard expression is not that egen doesn't allow it; it's that Stata does not allow it anywhere.

      With a small number of variables you could do something like

      Code:
      gen itchinessss = inlist(1, sideeffects_initial1, sideeffects_initial2, sideeffects_initial3)
      but that gets tedious quickly.

      Comment


      • #4
        Thank you both so much. Both of those solutions are excellent and I have saved them both in my Stata cheatsheet notebook for future reference.

        Comment

        Working...
        X