Announcement

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

  • Foreach in Do Edit

    Dear All,

    First post.... I'm looking to run a piece of code which involves a foreach command. I want to fully automate this code which is as follows: -

    Can anyone advise how I do this as I am getting tired of continually typing it into STATA. I've tried to run this using the Do Editor tool and get the following error....

    "foreach command may not result from a macro expansion interactively or in do files"

    Is there something that can be done to full automate me work?

    The codes as follows: -



    local y

    local r2=0




    . foreach var of local x {

    3. regress FXY `var'

    4 if( e(r2) > `r2'){

    5 local r2=e(r2)

    6. local y "regress FXY `var'"

    7. }

    8 }




    . di "`y’”







    . `y’




    local a

    local vif_1=0

    foreach var of local x {

    `y' `var'

    vif

    return list

    if(r(vif_1)<=1.5){

    local a `a’ `var'

    }

    }


    di "`a’”


    foreach var of local a {

    `y' `var’

    vif

    }


  • #2
    Welcome to Statalist.

    It is difficult to tell what your problem is. I cannot reproduce the precise error you obtained. At the end of this post is a version of your code, modified to run on one of the sample datasets installed with Stata. I created this code in the Do-file Editor window and successfully ran it from that window. Because of problems with your original code, I am not 100% certain this code is representative of what you were trying to accomplish.

    In creating this code I had to change a number of "smart quotes", highlighted in red in the samples below. I don't know if this problem was part of your original code, or if you had pasted your code into something like Word on its way to

    Code:
    local a `a `var'
    to
    local a `a' `var'
    
    di "`a’”
    to
    di "`a'"
    In general, it is a bad idea to put Stata code into Word or similar "writing" applications, since Stata's use of quotation marks differs greatly from standard usage in most non-computer languages.

    With that said, please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question, and on how to effectively present sample code and output using CODE delimiters, as I have done in this post.

    The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

    Here's the version of your code I ran successfully from the Do-file Editor window.

    Code:
    sysuse auto, clear
    
    local y
    local r2=0
    local x weight price
    
    foreach var of local x {
    regress mpg `var'
    if( e(r2) > `r2'){
    local r2=e(r2)
    local y "regress mpg `var'"
    }
    }
    
    display "`y'"
    `y'
    
    local a 
    local vif_1=0
    
    foreach var of local x {
    `y' `var'
    vif
    return list
    if(r(vif_1)<=1.5){
    local a `a' `var'
    }
    }
    
    di "`a'"
    
    foreach var of local a {
    `y' `var'
    vif
    }

    Comment


    • #3
      William,

      a big thank you i've used a modified version of your code and it is all working. I'd copied the code from notes on the mac into do editor i wonder if there was an issue there as you have e described.

      Giles

      Comment


      • #4
        The problem with Notes is that it adheres to the Keyboard System Preferences. In System Preferences, click on Keyboard, then on the Text tab, and note that "Use smart quotes and dashes" is checked. I expect the same would be true of TextEdit and other Apple apps.

        Comment

        Working...
        X