Announcement

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

  • How to use putexcel to take a single value of different variables in the data set?

    Hi,

    In my data set, I have a control and treatment group. I am trying to take the mean, standard error, and p-value for them into an excel sheet. I used a loop for this:

    Code:
    foreach x in N_baseline {
    qui gen `x'_T1=`x' if vote==1
    qui gen `x'_T2=`x' if vote==0
    
    foreach k in T1 T2 {
    egen m_`k'=mean(`x'_`k') 
    local m_`k' = m_`k'
    egen sd_`k'=sd(`x'_`k') 
    local sd_`k'= sd_`k'
    }
    
    quietly reg `x' vote,  r
    quietly test vote = 0
    gen p_1= r(p)
    local p_1 = p_1
    
    qui g nb_T1=1 if `x'!=.&vote==1
    qui  egen N_T1=total(nb_T1)
    local N_T1 N_T1
    
    qui g nb_T2=1 if `x'!=.&vote==0
    qui  egen N_T2=total(nb_T2)
    local N_T2 N_T2
    
    
    display "`x'" ","  `N_T1'  ","  `m_T1' ","  `sd_T1' ","  `N_T2' "," `m_T2' ","  `sd_T2' "," ","  `p_1'  
    return list
    }
    The loop gave me the following new variables that I am posting using dataex. I tried to convert the different variables and their single value to a matrix using mkmat. However, it is not working. Any help would be highly appreciated.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(m_T1 sd_T1 m_T2 sd_T2 p_1 N_T1 N_T2)
    16.26087 5.543504 16.065218 5.174305 .86148 46 46
    16.26087 5.543504 16.065218 5.174305 .86148 46 46
    16.26087 5.543504 16.065218 5.174305 .86148 46 46
    16.26087 5.543504 16.065218 5.174305 .86148 46 46

  • #2
    Code:
    mkmat m_T1 sd_T1 m_T2 sd_T2 p_1 N_T1 N_T2, matrix(M)
    matrix list M
    A few comments on your post. First, the content of your question has no relationship to the title you gave the thread. This will be a problem for people who are trying to search for either one and will end up in the wrong place.

    Second, never say "it is not working" when asking for help. There are many ways in which something could fail to work, and you need to say what is going wrong. Did Stata crash? Did it give you an error message? If so, what was the message? Or did it run without error messages but produce results you think are wrong? If so, what were those results, and in what way do they differ from what you wanted?

    Third, even worse is to say "it is not working" without even saying what "it" is. We are left with two mysteries. You did say you used -mkmat-, but you didn't actually show the command you used. So there is no way to know why the resulting command failed. When asking people to troubleshoot your code, it is essential to always show the actual code you tried, and show it exactly as you tried it, and then show or describe what Stata did in return, and explain, unless it is blatantly obvious, why that isn't what you wanted.

    Comment


    • #3
      Dear Clyde Schechter,

      I am very sorry for not being clear. The putexcel command that I use gives me a blank excel file. Somehow I cannot select a particular row in the initial matrix. The small matrix s comes out to be an empty matrix. The code that I have used:

      Code:
      mkmat m_T1 sd_T1 m_T2 sd_T2 p_1 N_T1 N_T2, matrix(S) rownames(id)
      matrix list S
      matrix s = S[,1]
      putexcel set example1, sheet(sumstats) replace
      putexcel A1=matrix(S)

      Comment


      • #4
        The error is your attempt to extract the first row. The correct command for that would be:
        Code:
        matrix s = S[1, 1...]
        The first 1 designates the first row, and the 1... that follows the comma tells Stata to retain all of the columns.

        Also, to export only the submatrix s, then the final command should be
        Code:
        putexcel A1=matrix(s)

        Comment

        Working...
        X