Announcement

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

  • macro involving factor variables

    Hi all!

    I am new to Stata and I tried to find an answer before daring to post this question.

    I am trying to make things run smoother and I thought about having a macro involving factor variables.

    Something in the line of:

    Code:
    global depvar "y z t"
    global controls "ib2.x1 i.x2 c.x3"
    
    foreach depvar in $depvar {
        logit `depvar' $controls
        }
    I run the code in one take, but it doesnt work.
    What am I missing? I think it might have to do with syntax(fv), but I dont know how to work it into the code.

    Any suggestions much appreciated
    A.

  • #2
    "it doesn't work" is about as unhelpful as you can get. In what way didn't it work? I tried something analogous to your code using the built-in auto.dta data set and it runs without error messages:

    Code:
    clear*
    sysuse auto, clear
    
    global depvar foreign
    global controls ib5.rep78 c.price c.mpg
    
    foreach depvar in $depvar {
        logit `depvar' $controls
    }
    That said, there are some really bad coding practices in there. Within the same block of code, it is confusing at best to use the same name, depvar, to refer to do different things. Even though Stata's parser can distinguish them, it makes the code harder to test and maintain. Next, there is no reason to use global macros here. You are just making lists of variables: local macros are much safer. Globals are inherently dangerous and should be used only when there is no way to avoid them.

    I don't know what's wrong with your code, as I don't know what actually happened when you ran it. But it is possible that the use of globals snagged you. It is possible that some other code running at the same time has a global named depvar or controls and that your definitions got clobbered. Or it may have nothing to do with that.

    Here's a better way to code what you're doing:
    Code:
    local depvar y z t
    local controls ib2.x1 i.x2 c.x3
    
    foreach v of varlist `depvar' {
        logit `depvar' `controls'
    }
    Please read the Forum FAQ for excellent advice about how to post questions that maximize your chance of getting a timely and helpful response. Among the things you will learn there:

    1. Never say "it didn't work." Always show exactly what code you used and exactly what Stata gave you in response. If it isn't obvious, explain why that isn't what you were expecting.

    2. When asking for help with code it is usually advisable to show example data as well so that responders can test their solutions on a data set that resembles what you are working with. For this, you should use the -dataex- command. If you are running version 15.1 or a fully updated version 14.2, it is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment

    Working...
    X