Announcement

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

  • Nested foreach

    Dear all,

    I have been struggling with the
    Code:
    foreach
    command for the following task:

    Code:
    local Obv profit hkgovsme_result marketing sale_no moti_self
    local Sbv sex fail hkgovsme_app intelp esale fund_no esale_eu esale_none sale_no sorted_nation
    local Ocv num_busp ft pt gba_ft ebus_12m
    local Scv yr_bus pt num_busp buscha_uncertain hkgovsme gba_busyr
    
    foreach T of newlist Sb Sc{
    foreach n of local `T'v{
    local label: variable label `n'
    graph hbar, ///
    over(`n') over(tech) ///
    stack percent ///
    asyvars ///
    blabel(bar, position(center) format(%9.0f) color(white) size(2.5)) ///
    legend(r(1) size(2)) ///
    yline(50)  ///
    ytitle("%") ///
    b1title("`label'") ///
    note("Conditional observation: Startups")
    
    graph export `add'\`T'v_`n'.png, replace
    }
    }
    
    foreach T of newlist Ob Oc{
    foreach n of local `T'v{
    local label: variable label `n'
    graph hbar, ///
    over(`n') over(startup) ///
    stack percent ///
    asyvars ///
    blabel(bar, position(center) format(%9.0f) color(white) size(2.5)) ///
    legend(r(1) size(2)) ///
    yline(50)  ///
    ytitle("%") ///
    b1title("`label'") ///
    note("Conditional observation: ALL")
    
    graph export `add'\`T'v_`n'.png, replace
    }
    }
    As you may notice, the only difference between the two -foreach- is the following
    1. objects in -newlist(.)-: Sb, Sc and Ob, Oc
    2. the variables in the - over(.)-: tech, startup
    I would like to know if there is any more elegant way to get the same result? I encounter and struggle the similar problem often

    Thank you

  • #2
    As for the looping, this is a little simpler and works:

    Code:
    local Sbv sex fail hkgovsme_app intelp esale fund_no esale_eu esale_none sale_no sorted_nation
    local Scv yr_bus pt num_busp buscha_uncertain hkgovsme gba_busyr
    
    foreach T in Sb Sc {
       foreach n of local `T'v {
           display "`n'" 
       }
    }
    The problem however that I noticed was in lines like

    Code:
     
     graph export `add'\`T'v_`n'.png, replace
    The local macro add is not defined in the code you give, but the backslash is certainly wrong. See https://www.stata-journal.com/sjpdf....iclenum=pr0042 and its references.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      As for the looping, this is a little simpler and works:

      Code:
      local Sbv sex fail hkgovsme_app intelp esale fund_no esale_eu esale_none sale_no sorted_nation
      local Scv yr_bus pt num_busp buscha_uncertain hkgovsme gba_busyr
      
      foreach T in Sb Sc {
      foreach n of local `T'v {
      display "`n'"
      }
      }
      The problem however that I noticed was in lines like

      Code:
      graph export `add'\`T'v_`n'.png, replace
      The local macro add is not defined in the code you give, but the backslash is certainly wrong. See https://www.stata-journal.com/sjpdf....iclenum=pr0042 and its references.
      Dear Nick,

      Thank you for your reply. I am sorry for the missing information.

      The add is defined as follow:
      Code:
      local add C:\Users\econ_ias\Dropbox\CK\CK_Survey\graph\sig_v\
      Besides, the desired result is conditioning on startup (tech): processing the loop for variables in local macro Obv and Ocv (Sbv and Scv)

      My main concern is that if there are any alternatives that writing one foreach loop that respectively conditions on startup and tech and achieves the desired result.

      Any suggestion is appreciated!

      Thank you
      Last edited by Chris Lam; 05 Jun 2019, 22:39.

      Comment


      • #4
        So the local macro add would cause problems too, as its contents end with a backslash. You need minimally

        Code:
         
         local add C:\Users\econ_ias\Dropbox\CK\CK_Survey\graph\sig_v/
        Your main concern should be getting code that works.

        Once you fix the errors pointed out, which you don't mention in your reply, I don't see a way of reducing your code that is a good idea. Making it shorter in this case won't make it clearer.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          So the local macro add would cause problems too, as its contents end with a backslash. You need minimally

          Code:
          local add C:\Users\econ_ias\Dropbox\CK\CK_Survey\graph\sig_v/
          Your main concern should be getting code that works.

          Once you fix the errors pointed out, which you don't mention in your reply, I don't see a way of reducing your code that is a good idea. Making it shorter, in this case, won't make it clearer.
          Thank you, one silly question is that I do not understand the problem that backlash.. while I got errors if I do not have the extra backslash in the export syntax if I keep the path of macro local add unchanged, see the following:
          Code:
           
           graph export `add'\`T'v_`n'.png, replace
          Besides, so you mean there is no way of having a conditional item/variable in a loop?
          I have encountered this problem many times.. for example in the loop for some variable I would like to condition the observation on b but some on a to draw the graph/table/regression.

          Comment


          • #6
            I should not be dogmatic. Looking at this again, I see scope for rearranging. If this is closer to what you want, then go for it. Not tested and I am ignoring add.

            Code:
            local Sbv sex fail hkgovsme_app intelp esale fund_no esale_eu esale_none sale_no sorted_nation
            local Scv yr_bus pt num_busp buscha_uncertain hkgovsme gba_busyr
            local Obv profit hkgovsme_result marketing sale_no moti_self
            local Ocv num_busp ft pt gba_ft ebus_12m
            
            local options  over(tech) stack percent asyvars ///
            blabel(bar, position(center) format(%9.0f) color(white) size(2.5)) ///
            legend(r(1) size(2)) yline(50)  ytitle("%")
            
            foreach T in Sbv Scv Obv Ocv {
                local which = cond(inlist("`T'", "Sbv", "Scv"), "Startups", "ALL")
                foreach n of local T {
                     local label: variable label `n'
                    
                     graph hbar,  over(`n') b1title("`label'") `options' ///
                     note("Conditional observation: `which'")
            
                     graph export `T'_`n'.png, replace
                }
            }
            Note: Crossed with #5. I think something like

            Code:
            local add C:\Users\econ_ias\Dropbox\CK\CK_Survey\graph\sig_v  
            
            graph export `add`/`T'_`n'.png, replace
            should work.
            Last edited by Nick Cox; 06 Jun 2019, 04:18.

            Comment

            Working...
            X