Announcement

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

  • create a variable from 1x1 matrix elements

    Hello,

    I would like to create a variable from 1x1 matrix elements. Specifically, I have matrices A1, A2, ... A100, each of which is a 1x1 matrix. I would like to create a variable B where the first observation is A1's value, the second observation is A2's value, and so on, with 100th observation being A100's value. I just don't know the syntax (I don't have much experience with Stata's matrices). Please help.

    Thank you,
    Stan

  • #2
    Code:
    gen B = .
    forvalues i = 1/100 {
        replace B = A`i'[1,1] in `i'
    }
    This assumes that your data set already contains at least 100 observations. If it does not, you have to first -set obs 100-.

    Comment


    • #3
      Thank you, I didn't realize this requires a loop.

      I noticed that if my matrices are instead named A1001, A1002, ..., A1100, the following code does not work (error: 'j' invalid observation number):
      Code:
       gen B = .
      forvalues i = 1001(1)1100 {  
           scalar j = `i' - 1000 
           replace B = A`i'[1,1] in j
      }
      I don't understand what's wrong with the code. Any ideas? Thank you.
      Last edited by Stan Peterburgsky; 22 Aug 2019, 07:52.

      Comment


      • #4
        You would be better off using a local macro and then you need the surrounding quotation marks, just as in Clyde Schechter 's example in #2. Stata won't evaluate a scalar on the fly in that context without a strong push.


        Code:
        gen B = .  
        quietly forvalues i = 1001(1)1100 {        
            local j = `i' - 1000        
            replace B = A`i'[1,1] in `j'  
        }

        Comment


        • #5
          Thank you very much for your help!

          Comment

          Working...
          X