Announcement

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

  • Calculating stored values from list simultaneously and ordered

    I am stuck on how to program a list that I want to run at the same time (ie not a loop).

    Currently I have a code as such (simplified):

    reg gini gender age2 age3 if T==1 [aweight=w]
    scalar define gamma_hat = _b[_cons] + _b[gender]*mean_t1[1,1] + _b[age2]*mean_t1[1,2] + _b[age3]*mean_t1[1,3]
    I do not want to keep typing the variable and the number each time and would like automatize that aspect.

    What I would like to do is take each variable from local vars and calculate something like this:
    local vars gender age2-age3
    local i = 1

    reg gini `vars' if T==1 [aweight=w]
    scalar define q_gamma1_1_hat = _b[_cons] + _b[var1]*mean_t1[1,i] + _b[var2]*mean_t1[1,i+1] + _b[var3]*mean_t1[1,i+2]
    This obviously does not work, but I don't know enough about stata's programming language to know how to calculate the variables in order, but also simultaneously.

    Thank you for your help.

  • #2
    To reference the value of your local macro vars you have placed it within quotation marks
    Code:
    `vars'
    To reference the value of your local macro i you need to similarly place it within quotation marks, so that at a minimum you need syntax like
    Code:
    mean_t1[1,`i'+2]
    With that said, to improve your future posts, 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. See especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE] (the # icon on the Statalist Advanced Editor toolbar), as described in section 12 of the FAQ.

    If you are interested in improving your knowledge of Stata, when I began using Stata in a serious way, I started - as others here did - by reading my way through the Getting Started with Stata manual relevant to my setup. Chapter 18 then gives suggested further reading, much of which is in the Stata User's Guide, and I worked my way through much of that reading as well. All of these manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through Stata's Help menu. The objective in doing this was not so much to master Stata as to be sure I'd become familiar with a wide variety of important basic techniques, so that when the time came that I needed them, I might recall their existence, if not the full syntax, and know how to find out more about them in the help files and manual.

    Stata supplies exceptionally good documentation that amply repays the time spent studying it - there's just a lot of it. The path I followed surfaces the things you need to know to get started in a hurry and to work effectively.

    Comment


    • #3
      Thank you for your response. It seems I was unclear.

      Indeed, I meant to put the i with the proper quotations of `i'. I will simplify my question to go to the heart of my question (and remove the example of the list called i). I have read the manuals and did some research on statalist, but have not yet figured out how to solve this particular issue with stata as the way lists are constructed and how to recall them are different than how I use them in other programming languages.

      I recognize that I need to put the quotations to access the list, but I want to be able to access a name in a particular position in that list. Essentially, how do I access a list by position in a simultaneous calculation?

      For example,

      We have a list called vars with the names, gender, age2, age3 in position 1,2,3
      Code:
      local vars gender age2 age3
      I would like to recall that list named var with the positions so that it calculates:
      Code:
      scalar define q_gamma1_1_hat = _b[_cons] + _b[gender]*mean_t1[1,2] + _b[age2]*mean_t1[1,3] + _b[age3]*mean_t1[1,4]
      essentially I want to be able to do the below in theoretical terms:

      local vars position1 position2 position3
      scalar define q_gamma1_1_hat = _b[_cons] + _b[list var position1]*mean_t1[1,2] + _b[list var position2]]*mean_t1[1,3] + _b[list var position3]]*mean_t1[1,4]
      I know that you can do something like this (which I have used for simultaneous lists, but not accessing positions within the same list):

      Code:
      local vars gender age2 age3
      reg gini `vars' if T==1 [aweight=w]
      local n: word count `vars'
      forval v = 1/`n' {
      local a: word `v' of `vars'
      scalar define q_gamma1_1_hat = _b[_cons] + _b[`a']*mean_t1[1,1] + _b[`a']*mean_t1[1,2]
      }
      But, this puts a loop to repeat the same value of position 1 of the list called vars across the equation instead of accessing position 1 and position 2 of the same list called vars simultaneously.

      I hope this is a bit clearer in my objective. I greatly appreciate your advice.
      Last edited by Mary Kaltenberg; 09 Jun 2018, 19:03.

      Comment


      • #4
        Perhaps some this like this:

        Code:
        sysuse auto,clear
        local vars mpg weight gear
        local listlength: word count `vars'
        
        forv i = 1/`listlength' {
            local pos`i' : word `i' of `vars'
        }
        reg price `vars'
        scalar myscalar = _b[_cons] + _b[`pos1'] + _b[`pos2'] + _b[`pos3']
        scalar list myscalar

        Comment

        Working...
        X