Announcement

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

  • Results of loop not displaying

    Hello,

    I am trying to learn how to write loops to perform commands on multiple variables at once. In the following code, I try to write a loop that runs Wilcoxon's rank sum test on 14 continuous variables. The loop runs without error, but I can't figure out how to get the results to display. I don't know where they go/how to call them.

    My code:
    #local continuous gm_csf ifn_g il_2 il_7 il_5 il_10 il_13 il_15 il_17 tnf_a mcp_1
    #foreach var of local continuous {
    ranksum `var' if `var'!=0, by(sptb_new)
    }

    I have also tried to apply Matthew McKay's code from looking back in this forum on related topics:

    ** Program pe:
    Print Execute **

    #program define pe
    # version 12.0
    # if `"`0'"' != "" {
    # display as text `"`0'"
    '
    # `0' # display("")
    # }
    #end
    One can just prepend pe to each line within the foreach loop.

    #sysuse auto
    #levelsof rep78, local(levs)
    #foreach lev in `levs' {
    # pe regress price weight if rep78==`lev'
    #}


    Applying this, I wrote the following code:
    #program define pe version 12.0 if `"`0'"' != ""
    { display as text `"`0'"' `0' display("") } end
    #foreach var of local continuous {
    pe ranksum `var' if `var'!=0, by(sptb_new)
    }


    However, I am still not getting anything. The only output I see is:


    . foreach var of local continuous {
    2. pe ranksum `var' if `var'!=0, by(sptb_new)
    3. }

    .
    end of do-file

    Where are the results?

    Thank you!
    Clare
    Last edited by Clare McCarthy; 20 Sep 2019, 11:05.

  • #2
    I presume you didn't really have "#" in your code, and I'm puzzled by why you show them here. Setting that aside: When there's a problem like this, one diagnostic trick is to try the same thing on some stock data set. How does the following work for you?


    Code:
    clear
    sysuse auto
    local continuous weight headroom length turn
    foreach var of local continuous {
       ranksum `var' if `var'!=0, by(foreign)
    }

    Comment


    • #3
      Thanks, Mike. (I thought the # was standard for presenting code on this forum)

      That code on the stock dataset produced results for each variable just as you'd think it would. However, I'm still getting the same problem of no output when I return to my dataset.

      Comment


      • #4
        My guess: The key lies in the local macro. Where do you define it? Where are you using it? The name local is not mysterious or mischievous or mystifying jargon: it means what it says, so that a local macro is visible only locally, within the same program space, meaning in turn

        the same interactive session

        OR

        the same do-file or program (provided it is run all at once)

        OR

        a chunk or segment of a do-file or code in a do-file editor window (if it is run as one chunk or segment by selecting code).

        Note that it is not itself an error to refer to a local macro that doesn't exist. The definition of "doesn't exist" is epistemological, not ontological: it just means "not observable locally" just as there being no cat called Kitty observable in my house does not mean that no such cat exists elsewhere.

        So I can go

        Code:
        foreach k of local nonexistent {
              di "`k'"
        } 
        and nothing will happen -- whenever it is true that I defined no such local with that name in the same space, The code is legal but boils down to a request to do nothing and Stata just sits there quietly doing nothing and is not bored or offended.

        I should stress briefly that a side-effect of referring to a local macro that does not exist may well be a statement that on other grounds is illegal, but I stop there.

        So the bottom line is the top line:

        The key lies in the local macro. Where do you define it? Where are you using it? If in different places, that is the problem.

        Comment


        • #5
          Thanks so much, Nick! My lack of results was indeed in misunderstanding the local command. I had not been running it in the same execution as the foreach loop. I had been running it separately right before. Running it in the same execution is now producing results for all of my foreach loops.

          Comment


          • #6
            just to clarify the "#" - the way to put material into a code block, thus making it easily legible, is to click on the "#" in the "tool" bar just above where you are typing; however, do not actually type a "#" - please read the FAQ for more on this

            Comment

            Working...
            X