Announcement

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

  • How to run a loop with exclusion list

    Dear all,

    I am trying to create a new variable PTAextramother depending on the values of two other variables PTA_*_i and PTA_*_j (which could be equal to 0 or 1). The "*" corresponds to different numbers ranging from 1 to 955.
    Therefore I tried this code:
    Code:
    forval v=1/955 {
          capture confirm var PTA_`v'_j
          if _rc == 0 {
     gen PTAextramother`v' = 1 if PTA_`v'_i == 1 & PTA_`v'_j != 1
    }  
    }
    This code works, but not for what I would like to do, i.e. :

    - The first problem, is that I would like to exclude some PTA_*_i (and PTA_*_j) from the list (so for instance in the range 1/955, I would like to exclude : 2, 4 and 6), I don't know how to do that.
    - the second problem is that I would like to generate a variable PTAextramother instead of PTAextramother`v' (which will take into account all PTA from 1 to 955). But I have a message error saying PTAextramother is already defined if I do it the way I want....

    Could anyone help on those 2 points?
    Many thanks,

  • #2
    Code:
    gen PTAextramother = 0 
    foreach v of numlist 1 3 5 7-955 { 
         capture replace PTAextramother = 1 if PTA_`v'_i == 1 & PTA_`v'_j != 1
    }

    Comment


    • #3
      Thank you Nick for your help. Could there be a more generic way to exclude some numbers, as I just gave an example, but I may have other numbers to exclude? Like it would be take all numbers except <2 4 6 300 450 ...>
      Anyway, the code you sent will work perfectly.

      Comment


      • #4
        How about the following?
        Code:
        foreach v of numlist 1 3 5 7/299 301/449 451/955 {
        Last edited by William Lisowski; 18 Jan 2018, 07:46.

        Comment


        • #5
          As William's answer implies, there is no white magic here. If you want to exclude certain numbers, you need to say what they are one way or another.

          However, if this were my problem, I'd expect that there would be a better solution than spelling out a complicated numlist (which is likely to be tedious, error-prone and harder to understand later or to rewrite for a similar problem). You might say loop over a varlist or use capture (as you are already doing, but perhaps it can be extended).

          Comment


          • #6
            Thank you to both of you. I will stick with the solution you were suggesting. I thought I could use unab varlist and unab exclude to create my list of default variables, but it seems not feasible in my case.

            Comment


            • #7
              Perhaps this. See the appropriate help files for documentation, but the numlist command is documented at help nlist .
              Code:
              . numlist "1/20"
              
              . local all `r(numlist)'
              
              . numlist "2 4 6 11/15 18"
              
              . local exclude `r(numlist)'
              
              . local include : list all - exclude
              
              . display "`include'"
              1 3 5 7 8 9 10 16 17 19 20

              Comment


              • #8
                Thank you, it is what I was looking for.

                Comment

                Working...
                X