Announcement

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

  • Loop in another loop

    Hi I am trying to make a loop inside another loop but it does not work. What's wrong with my code?

    Code:
    foreach var of varlist stock_implants stock_iud stock_sayana_press stock_depo_provera stock_pills stock_male_condoms stock_female_condoms stock_ec stock_diaphragm stock_foam stock_beads stock_injectable {
    
    gen no_`var'=1 if `var'>1 & `var'<.
    
       foreach x of varlist no_`var'{
       
    recode no_`var' (.=0) if fp_offered=1
    
    }
    }
    Saleh

  • #2
    It would be simple for us to answer if you paste an example of your data set. One potential issue might be with this line
    Code:
     foreach x of varlist no_`var'{
    change this the following. i.e.
    Code:
    foreach x of varlist no_*{ 
     recode `x' (.=0) if fp_offered=1
    Again, without seeing the data, it is just an educated guess.
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

    Comment


    • #3
      For once the data seem irrelevant to the syntax.

      This can be slimmed down. First, correct the test = 1 to == 1 as otherwise the code will fail.

      Now note that the inner loop is a loop over one variable, namely reducible to a single statement.

      Each new variable will be 1 if

      Code:
      `var' > 1 & `var' < . 
      and otherwise 0 if

      Code:
      fp_offered == 1
      and otherwise missing.


      That all can be rewritten


      Code:
      foreach var of varlist stock_* {    
          gen no_`var' = cond(`var' > 1 & `var' < ., 1, cond(fp_offered == 1, 0, .))  
      }
      I am guessing here that the wildcard stock_* expands to the variables listed; if that is not true then Saleh does indeed need to use the list of variable names he spelled out.

      Now whether you use
      cond() is a matter of programming taste. Some people find nested conditions hard to read but the trick is to think through them in words in some fashion like the sentence I have put in bold above. (If bold does not show up in your mailer, it is the sentence starting Each variable ....)

      For a tutorial on cond() see http://www.stata-journal.com/sjpdf.html?articlenum=pr0016 and for an example in which three nested cond() calls are natural see http://www.stata.com/support/faqs/data-management/leap-year-indicators/

      As in elementary algebra the parentheses ( ) must match.

      If despite this advocacy you still find two statements clearer, go for it:

      Code:
      foreach var of varlist stock_* {
            gen no_`var' = 1 if `var' > 1 & `var' < .
            replace no_`var' = 0 if `no_var' == . & fp_offered == 1
      }
      Avoiding recode whenever possible as I tend to do is also arbitrary taste. Those who have spent a lot of time around some other statistical packages tend to regard it as a favourite friend.

      But -- to return to the title of the thread -- there is no need for two loops at all.
      Last edited by Nick Cox; 07 Apr 2017, 17:21.

      Comment


      • #4
        I appreciate your input Nick.
        Thanks Attaullah.

        Comment


        • #5
          Nick, I've posted a similar inquiry except mine issue involves three loops in one and a outreg2 issue. Is it possible that you could please look at my post and provide feedback????

          Comment


          • #6
            Kasim: I would look at it if I knew where it was posted. If you're implying a personal email then please know in advance that I typically decline to answer Stata-related questions sent to me privately by people I don't know unless they concern bugs or limitations of my own programs.

            As an extension of http://www.statalist.org/forums/help#adviceextras #2 it's important to underline that private queries answered privately are of limited or no benefit to anybody else whereas the whole point of a forum is that others can find threads interesting or useful. Equally crucial, any question can benefit from the ideas of several and a thread here is less subject to the vagaries of who is asleep, busy, enjoying a hard-earned break or otherwise unavailable.

            There is also the small limitation that my time is finite. I suspect that other highly active people would have similar personal policies.

            Crucial detail: I never use outreg2 (SSC) and have no interest or expertise in answering questions about it. Note that the author is not a member here.
            Last edited by Nick Cox; 13 Apr 2017, 01:39.

            Comment


            • #7
              I see that Kasim was alluding to http://www.statalist.org/forums/foru...oping-sequence

              Comment

              Working...
              X