Announcement

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

  • How to write this with a loop (repetitive coding with numerical values that do not follow each other)

    Hello everyone,

    How can I write this faster?

    Code:
    svy: prop rech if form1==2 & gan1==14
    svy: prop rech if form1==2 & gan1==12
    svy: prop rech if form1==2 & gan1==8
    svy: prop rech if form1==2 & gan1==6
    svy: prop rech if form1==2 & gan1==5
    svy: prop rech if form1==2 & gan1==4
    svy: prop rech if form1==2 & gan1==3
    svy: prop rech if form1==2 & gan1==2
    svy: prop rech if form1==2 & gan1==1
    
    svy: prop rech if form1==3 & gan1==14
    svy: prop rech if form1==3 & gan1==12
    svy: prop rech if form1==3 & gan1==8
    svy: prop rech if form1==3 & gan1==6
    svy: prop rech if form1==3 & gan1==5
    svy: prop rech if form1==3 & gan1==4
    svy: prop rech if form1==3 & gan1==3
    svy: prop rech if form1==3 & gan1==2
    svy: prop rech if form1==3 & gan1==1
    
    
    svy: prop rech if form1==1 & gan1==14
    svy: prop rech if form1==1 & gan1==12
    svy: prop rech if form1==1 & gan1==8
    svy: prop rech if form1==1 & gan1==6
    svy: prop rech if form1==1 & gan1==5
    svy: prop rech if form1==1 & gan1==4
    svy: prop rech if form1==1 & gan1==3
    svy: prop rech if form1==1 & gan1==2
    svy: prop rech if form1==1 & gan1==1
    Any suggestions please? Is it possible to produce a loop, despite the fact that the "gan1" variable numerical values do not follow each other?

    Thank you so much.

    Michael

  • #2
    Code:
    foreach i in 2 3 1 {
        foreach j in 14 12 8 6 5 4 3 2 1 {
            svy: prop rech if form1==`i' & gan1==`j'
        }
    }
    Looping over an explicit list can look inelegant but still be highly practical.

    The help and manual entries for foreach and forvalues are perhaps a little terse and I don't think it would be unfair to say that they seem pitched at a reader already familiar with loops, just as occasional uses of calculus in the manuals don't break off to give you miniature tutorials explaining derivatives and integrals. But foreach ... in ... is explicit as an allowed form.

    I wrote a couple of tutorial surveys of loops in 2002 and 2003 and then revised them again several years later. Here are the recent versions:

    Code:
    . search loops, sj
    
    Search of official help files, FAQs, Examples, and Stata Journals
    
    SJ-21-4 pr0075  . . . . . . . . . . . . . .  Speaking Stata: Loops in parallel
            . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
            Q4/21   SJ 21(4):1047--1064                              (no commands)
            tutorial discussing looping in parallel using foreach
            and forvalues
    
    SJ-21-2 pr0074_1  . . . . . .  Erratum: Speaking Stata: Loops, again and again
            . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
            Q2/21   SJ 21(2):555                                     (no commands)
            erratum for a line of code
    
    SJ-20-4 pr0074  . . . . . . . . . . . . Speaking Stata: Loops, again and again
            . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
            Q4/20   SJ 20(4):999--1015                               (no commands)
            provides updated guidance on using foreach and forvalues for
            looping through lists of values and repeating commands using
            members of those lists in turn
    If you run that search you will see more hits than are listed above.

    All that said, this may or may not be an alternative


    Code:
    statsby, by(form1 gan1): svy: prop rech

    Comment


    • #3
      Code:
      svy : prop rech, over(form1 gan1)
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        What beautiful material.

        Thank you so much for all this!
        I'm struggling to do this in 140 lines, and you do it in one line! It's wonderful.

        Thanks Nick Cox and Maarten Buis !

        ​​​​​​​Michael

        Comment


        • #5
          Dear Nick Cox :

          I think the statsby command is not suitable with svy. Here is what I obtained:

          Code:
          svy is not supported by statsby
          r(199);

          Comment


          • #6
            That's one to you. I said it might not work.

            Comment


            • #7
              Nick Cox

              ​​​​​​​Okay, great.
              Thank you very much for the suggestion and I apologise for the misunderstanding.

              Comment


              • #8
                No misunderstanding; you were right, and I was trying to cover myself given that I had not tried the code. If anyone wanted to say or think "Read the fine help" they would be right.

                Comment


                • #9
                  Thanks again Nick!

                  I have one more curiosity about the code posted in #2:
                  • What's the difference between foreach i of and foreach i in ?
                  Michael

                  Comment


                  • #10
                    The new question in #9 is just the difference between

                    items in an itemised list, which follows,

                    and

                    items in a list of a particular type, to be worked out otherwise, by looking up a varlist, working out a list of new variable names, looking in a local or global macro, or by expanding a numlist.

                    I am struggling to explain it more clearly than does the help.

                    Comment


                    • #11
                      Michael Duarte Goncalves just noting for future reference that if you do not want to write out all the elements of your numlists explicitly, and assuming you want to loop over all the values of your variable, you could check out
                      Code:
                      help levelsof
                      and use the stored macro.

                      Comment


                      • #12
                        Thanks Nick Cox for the clarifying explanation!

                        Thanks for the suggestion Matthew Lala

                        Comment


                        • #13
                          Also, if 14 12 8 6 5 4 3 2 1 are just arbitrary, but also possibly have some meaning, the best of both worlds can be to map them to integers 1 up but keep those values as labels.

                          A fairly complete discussion would take a longish paper but https://www.stata.com/support/faqs/d...-with-foreach/ discusses some alternatives here.


                          Note particularly the group() function of egen and its label option.

                          FWIW, I was the first author of levelsof and had to struggle a little to convince StataCorp that such a command was a good idea. Wait 20-odd years and I have the melancholy pleasure of thinking it more often used than it deserves. I have seen levelsof used to discover that a variable has 3 levels which should have been known in advance. But I guess that Matthew Lala is not encouraging its use whenever alternatives are trivially easy.
                          Last edited by Nick Cox; 30 Nov 2022, 06:11.

                          Comment


                          • #14
                            Nick Cox : Thanks for the stata link! I took a look at it: the page went straight to my favourites.

                            Best regards,

                            Michael

                            Comment

                            Working...
                            X