Announcement

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

  • Storing scalars into a vector - help with a loop

    Good evening to everyone,

    I need to basically append an empty list: after a loop in which I create some scalars, at the end of each scalar creation I would love to plug it into a cell. I think it should be possible with some combinations of
    Code:
     generate
    and
    Code:
     replace
    , but I can't figure out how to solve the issue... Here is the code:

    Code:
    forvalues i = 2(1)48{
        count if Z_`i' == 1 & Prom__20161 >= 4 & Prom__20161 !=.
        scalar NZ_`i'_1 = r(N)
        count if Z_`i' == 1 & Prom__20161 !=.
        scalar NZ_`i'_1_tot = r(N)
        scalar P_Y1_4_Z_`i'_1 = NZ_`i'_1 / NZ_`i'_1_tot
        
        count if Z_`i' == 0 & Prom__20161 >= 4 & Prom__20161 !=.
        scalar NZ_`i'_0 = r(N)
        count if Z_`i' == 0 & Prom__20161 !=.
        scalar NZ_`i'_0_tot = r(N)
        scalar P_Y1_4_Z_`i'_0 = NZ_`i'_0 / NZ_`i'_0_tot    
        scalar DIFF_`i' =  P_Y1_4_Z_`i'_1 - P_Y1_4_Z_`i'_0
        di DIFF_`i'
    }
    I would simply love to plug the values of DIFF_`i' into a column vector.

  • #2
    Perhaps this will point you in a helpful direction.
    Code:
    . matrix a = (.)
    
    . forvalues i = 2/5 {
      2.         local diff = 5 * `i'
      3.         matrix a = (a \ `diff')
      4. }
    
    . matrix list a
    
    a[5,1]
        c1
    r1   .
    r2  10
    r3  15
    r4  20
    r5  25
    For more details on Stata's matrix commands start with the output of help matrix and follow the links. Do note that Stata's matrix commands are separate from Stata's Mata language, which also concerns itself with matrices, and much else.

    Comment

    Working...
    X