Announcement

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

  • Looping a egen command

    Hello,

    I'm trying to learn loops but I have a problem when converting my long list of commands into a loop. I hope someone is willing to help me.
    I am using Stata 12.

    I used a long list of commands to return any variable between brackets from var A, then the same for B in an attempt to extract these values from var A into a new variable. Now I like to learn how to put this in a loop but its frustrating that I don't know how to do this. Is there anyone willing to show me how I can but the following commands into 1 loop?

    What I have is the following:
    * egen coreA1 = anyvalue(A), v(29 88 162 192 266 152)
    * egen coreB1 = anyvalue(B), v(29 88 162 192 266 152)
    * gen cpA=B if .>coreA1>0
    * gen cpB=A if .>coreB1>0
    * gen coreA=max( coreA, cpB)
    * gen coreB=max( coreB, cpA)
    * drop coreA1 coreB1 cpA cpB

    ------------------------------------
    A B coreA1 cpA
    75 162
    152 145 152 145
    152 226 152 226
    220 192
    243 33
    -------------------------------------

    Any help is much appreciated, thanks!


  • #2
    Code:
     if .>coreA1>0
    won't do what you want. Let's try

    Code:
    . di . > -1 > 0
    1
    That's wrong in terms of what you want as -1 is not positive. Why is Stata saying otherwise? The expression is being evaluated left to right, as if it were

    Code:
    . di (. > -1) > 0
    1
    You need instead

    Code:
    if (coreA1 > 0) & (coreA1 < .)
    except that in your case the first condition is redundant.

    That leaves your main question. You want a loop, but you don't give the variable names over which you want to loop, so a structure cannot be easily be guessed. Evidently your variables come in pairs, so that needs to be clear too.



    Comment


    • #3
      It is also worth noting that

      Code:
      gen coreA=max( coreA, cpB)
      will not work because coreA apparently already exists (since it is in the expression on the right side of the equal sign), so replace rather than generate is required.

      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, looking especially at sections 9-12 on how to best pose your question. It would be particularly helpful to post a small hand-made example, perhaps with just a few variables and observations, showing the data before the process and how you expect it to look after the process. The FAQ suggests posting examples of the code you've tried and what Stata reported, but based on Nick's and my observations, it appears the code you posted hasn't actually been tried.

      Regarding the loop, it it your intent to loop over pairs of variables like A and B? That is, do you essentially have three lines of code that you want to replicate for A and B, C and D, ...?

      Again, a small example of your data and your desired results, posted using dataex as described in the FAQ, would help communicate what you hope for and give the reader some data to test on. Use the keep command to reduce the number of observations and variables in a copy of your data, and then use the data editor to fill in a variable with the desired results.
      Last edited by William Lisowski; 30 Apr 2016, 08:10.

      Comment


      • #4
        Thank you all for the fast response, I try to make my post more clear. The dataset I use are from the interbank market and show transactions, variable A consists of bank numbers with counterparty banks in variable B of a particular transaction.

        What I try to do is extract all the core banks that are identified under the exact numbers (29 88 162 192 266 152) in both variable list A en B.
        I try to extract only the lines that match to any of the values between these brackets, anything else may be omitted. If variable A matches this value then any value under B has to be extracted as well under new variable 'cpB'. As this firm is the counterparty of the identified core bank in variable A in this transaction, and visa versa for any core bank in variable B with counterparty A respectively.

        So yes like William says I try to loop over 2 variables (which are pairs) searching for the numbers above and generate a new set of variables identified as 'coreA' and 'coreB'.

        Now I did run the code and it works fine without errors, so my question is not how to fix the codes I currently have. But how I can rewrite this into a looping command over variable A and B. As this is more efficient and I like to learn how to receive the same results with loops.

        Again thank you for any help

        Comment


        • #5
          As William says in #3, there is no way the code can work as specified. Please follow the directions in the FAQ to post an example of your data using dataex, and the exact commands and output from Stata in [CODE][/CODE] delimiters
          Stata/MP 14.1 (64-bit x86-64)
          Revision 19 May 2016
          Win 8.1

          Comment


          • #6
            Hi,
            I like to thank you all for trying to help me, with exploring other posts on this forum I managed to find the solution to my own problem. I think it can't harm posting my own solution here, sharing is caring

            I first generating two variables:
            Code:
            generate coreA=.
            generate coreB=.
            Then replacing all values with a loop to all matching values in var A and B
            Code:
            foreach x of numlist 29 88 162 192 266 152 {
                replace coreA = A if A == `x'
                replace coreA = A if B == `x'
                replace coreB = B if A == `x'
                replace coreB = B if B == `x'
            }

            Comment


            • #7
              You can make that shorter.

              Code:
                
              generate coreA=.
              generate coreB=.
                
              foreach x of numlist 29 88 162 192 266 152 {    
                   replace coreA = A if inlist(`x', A, B)      
                   replace coreB = B if inlist(`x', A, B)  
              }

              Comment


              • #8
                Hi Nick,
                I have a similar problem. I am using data from the wealth and assets survey UK to analyse the impact of past stock market experiences on stock market participation of households.
                I have historical stock returns data and the age of each person and I am trying to calculate 3 things:
                Recent returns: average of the stock market returns of the recent third of the individuals life
                Mid returns: average of the stock market returns of the middle third of the individuals life
                Past returns: average of the stock market returns of the early third of the individuals life.

                I have merged the lagged returns data based on the year that each individual gave the survey. so my data looks something like this:
                case year age rets rets1 rets2 rets3
                1 2009 60 5.1 2.7 -1.5 -2.0
                my lagged returns go all the way up until the 100th lag.
                So for example if the individual is 60years old and gave the survey in 2007
                recent returns would be the average of the first 20 lags of returns data
                mid returns would be the average of the next 20 lags
                early returns would be the average of the last 20 lags

                I created a variable called onethirdage that is:
                onethirdage=age/3
                and then tried to create a forval loop,
                I have tried to use this command:

                recent returns:

                local N = _N
                forvalues i = 1(1)`N' {
                local a = onethirdage-1 in `i'
                egen sumrets1=rowtotal(rets1-rets`a')
                }
                gen earlyrets=sumrets1/onethirdage

                I do not have much knowledge of stata and this command only gives me the value of the first observation and then gives the error "variable sumrets1 already defined"

                would love some help with this!

                Thank you

                Comment

                Working...
                X