Announcement

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

  • List string values of multiple varlists

    Hi everyone,

    I am trying to list the string values of two varlists (med_other_ind* and med_name*, both * = 1 to 20) with the coherent results next to each other as well as the variables id and event, if med_other_ind* contains specific pieces of text ("vit", "tilskud", "osteo" or "forebyg"). I have 20 coherent varlists, and I could do like this:

    list med_other_ind1 med_name1 if strpos(med_other_ind1, "vit") | ///
    strpos(med_other_ind1, "tilskud") | strpos(med_other_ind1, "osteo") | ///
    strpos(med_other_ind1, "forebyg")
    list med_other_ind2 med_name2 if strpos(med_other_ind2, "vit") | ///
    strpos(med_other_ind2, "tilskud") | strpos(med_other_ind2, "osteo") | ///
    strpos(med_other_ind2, "forebyg")

    and so on with 20 repetitions in total.

    But there must be an easier or smarter way to do it :-)

    I have tried with both foreach and forvalues commands, but I only know how to incorporate one of the varlists into the commands. Then I do not get the coherent results as I do with the many commands described above - I get all results from both in a big mess or en error in STATA, eg.

    foreach x of varlist med_other_ind* {
    list id event med_name? `x' if strpos(`x', "vit") | strpos(`x', "tilskud") ///
    | strpos(`x', "osteo") | strpos(`x', "forebyg")
    }

    This gives med all the med_name results, and not the one coherent to the med_other_ind* (eg. med_name4 and med_other_ind4)

    And this code does not work:

    local varlist1 "med_other_ind* med_name*"
    forvalues med_other_ind = 1/20 {
    list `med_other_ind' if strpos(`x', "vit") | strpos(`x', "tilskud") ///
    | strpos(`x', "osteo") | strpos(`x', "forebyg")
    }

    It was my best attempt after having read this post:
    https://www.statalist.org/forums/for...=1569577824204


    So please, can anybody help me? Thank you in advance!

    Regards, Thomas Prior
    Last edited by Thomas Prior; 27 Sep 2019, 04:19.

  • #2
    The loop you seem to need is a loop over integers, e.g.

    Code:
    forval x = 1/20 {
    
    list med_other_ind`x' med_name`x' if strpos(med_other_ind`x', "vit") | ///
    strpos(med_other_ind`x', "tilskud") | strpos(med_other_ind`x', "osteo") | ///
    strpos(med_other_ind`x', "forebyg")
    
    }
    Your loop over integers

    Code:
    forvalues med_other_ind = 1/20 {
    
    list `med_other_ind' if strpos(`x', "vit") | strpos(`x', "tilskud") | strpos(`x', "osteo") | strpos(`x', "forebyg")
    
    }
    will fail at the first substitution

    Code:
    list 1 ...
    as 1 is not a variable name. It would also fail if local macro x were never defined.

    In some circumstances I would be tempted to do something more like this:

    Code:
    forval x = 1/20 {
    
    local X med_other_ind`x'
    
    list `X' med_name`x' if strpos(`X', "vit") | strpos(`X', "tilskud") | strpos(`X', "osteo") | strpos(`X', "forebyg")
    
    }


    Last edited by Nick Cox; 27 Sep 2019, 04:40.

    Comment


    • #3
      Thanks a lot - that was just what I needed. It does the job!

      Comment

      Working...
      X