local Nobs = r(N) local numitems = wordcount("$col_vars $row_vars") // count variables matrix C = J(`numitems',`numitems',.) // empty matrix where are going to be saved significance stars *Now begin to count how many stars are required by p-values /* p-value <= 0.05 => 1 star p-value <= 0.01 => 2 stars p-value <= 0.001 => 3 stars */ forvalues m = 1(1)`numitems'{ // m row index forvalues n = 1(1)`numitems'{ // n column index mat C [`m',`n'] = 0 if B[`m',`n']<=0.05 { mat C [`m',`n'] = 1 } if B[`m',`n']<=0.01 { mat C [`m',`n'] = 2 } if B[`m',`n']<=0.001 { mat C [`m',`n'] = 3 } } } matrix D = J(`numitems',`numitems',.) // D matrix round coef. correlation values forvalues m = 1(1)`numitems'{ // m row index forvalues n = 1(1)`numitems'{ // n column index mat D [`m',`n'] = round(A [`m',`n'],0.001) } } svmat C // turn C matrix in variables in dataset svmat D // turn D matrix in variables in dataset forvalues i = 1(1)`numitems'{ // create a Z matrix as variables converting D matrix in strings gen Z`i'="" } forvalues i = 1(1)`numitems'{ replace Z`i'=string(D`i',"%9.3f") } forvalues i = 1(1)`numitems'{ // create a X matrix with stars strings by how many stars are required gen X`i'="" } forvalues i = 1(1)`numitems'{ replace X`i'="" if C`i'==0 replace X`i'="*" if C`i'==1 replace X`i'="**" if C`i'==2 replace X`i'="***" if C`i'==3 } forvalues i = 1(1)`numitems'{ // S matrix concatenate Z (coef.) and X (stars) string matrices egen S`i'=concat(Z`i' X`i') } keep S* // Keep S matrix in dataset, it will be exported in Excel drop if S1=="." // Drop unnecesary observations gen Variable = "" // First column with variables names of exported matrix order Variable, first // Order this column at the begining of matrix local aux1 = `numitems'+1 // this index the last row of result matrix expand 2 in 1 // generate a new row with random values (are stored as observations) forvalues j=1(1)`numitems'{ replace S`j'="" in `aux1' // replace last row with an empty string } local aux2 = `numitems'+2 expand 2 in 1 forvalues j=1(1)`numitems'{ replace S`j'="" in `aux2' // make the same in a new row } replace Variable = "Prueba: $typetest" in `aux1' // add a legend with matrix title at bottom of the matrix replace Variable = "Legend: * p<0.05, ** p<0.01, *** p<0.001" in `aux2' // add legend of stars significance at bottom of the matrix local j = 1 foreach i in $row_vars $col_vars { // now assing variables names at first column replace Variable = "`i'" in `j' local j = `j'+1 } local j = 1 foreach i in $row_vars $col_vars { // now assing variables names at first row rename S`j' `i' local j = `j'+1 } gen Obs = . // where is shown number of observations of sample replace Obs = `Nobs' in 1 *Now drop uninterested data and keep only submatrix placed at the up-right of the whole correlation matrix drop $row_vars // drop correlations between subsales of the same test ('asq' & 'asq') foreach aux in $col_vars { drop if Variable=="`aux'" // drop correlations between subsales of the same test ('bay' & 'bay') }