Announcement

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

  • Loop for many, same regressions with similar names

    Hello everyone, I am new to Stata and need your help.
    I can run the following simple regression with the attached dataset:


    clear all

    import excel "xyz1.xlsx", firstrow case(lower) clear

    gen lxa = ln(20+xa)
    reg y lxa za


    In the dataset, I have the following variables:
    t y xa xb xd xf za zb zd zf


    I would like to repeat the regression as follows:
    gen lxb = ln(20+xb)
    reg y lxb zb


    ......

    gen lxd = ln(20+xd)
    reg y lxd zd

    ......

    gen lxf = ln(20+xf)
    reg y lxf zf

    ......

    How may I write a loop over 'a', 'b', 'd', 'f' to do the above task?

    Thank you very much!!


    Attached Files

  • #2
    This may work:

    Code:
    foreach v in a b d f{
    capture drop lx`v'
    gen lx`v' = ln(20 + x`v')
    reg y lx`v' z`v'
    }

    Comment


    • #3
      Code:
      local letters a b d f
      foreach l of local letters {
          gen lx`l' = ln(20+x`l')
          reg y lx`l' z`l'
      }
      Do read -help foreach- for more information and examples.

      By the way, the Forum FAQ advises that attachments are discouraged--many people, myself included, will not download attachments from people we do not know. Also, the correct solution to a problem often depends on details of the way Stata organizes your data, so that a spreadsheet or table does not provide the necessary information. So shared data should always come from a Stata data set, and needs to include important metadata. The most useful way to accomplish this is with the -dataex- command. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- 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.

      Added: Crossed with #2 which uses a different aspect of -foreach-.

      Comment


      • #4
        Note to anyone interested: John Baxter is evidently new to Stata and Statalist and asking lots of questions. That's fine, so far, but before you answer John, look around for his other threads: the different threads are mostly variations on each other.

        That's now three times at least that we've wasted effort explaining why not to post spreadsheet attachments, which is explained in the FAQ Advice any way.

        Comment

        Working...
        X