Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Keeping values in upper triangle of matrix

From   Michael Betz <>
To   "" <>
Subject   st: Keeping values in upper triangle of matrix
Date   Tue, 6 Dec 2011 22:30:35 +0000

Hi all,

I have a data set of 3000 US counties and about 25 variables of county characteristics. For every possible county pair, I need to calculate the difference in every variable. The way I have been doing this is for each variable, I create a matrix in Mata and element subtract the transpose.  The upper triangle of the matrix contains the values I need. I send the matrix back to Stata. I then save the observations in the upper half of the matrix in each column as its own data set and append each file. The problem is this takes about an hour to do. I'm looking for a more efficient way to accomplish this task. I wondered if there was a way to save all the elements of the upper triangle of the matrix as a column vector. This would save me the computing time of saving and appending 3000 files for each variable.



My current code is as follows:

foreach x of varlist amenityscale-wages{
use "Merged1993", clear
mkmat `x'

mata: a=st_matrix("`x'")
mata: b=J(3000,3000,1)

mata: c=b:*a
mata: d=c:-a'
mata: st_matrix("z", d)

svmat z
keep z*
save "Matrix1", replace

local i=1
cd "C:\Users \Append"
foreach y of varlist z*{
	use "C:\Users \Matrix1.dta"
	keep in f/`i'					//keep only upper half of matrix
	keep `y'						//keep only one variable
	ren `y' `x'					
	save `y', replace
	local i=`i'+1

save "C:\Users\ appendmat.dta", replace	
erase z3000.dta
local dtafiles: dir . files "*.dta"
	foreach file of local dtafiles {	
	append using "`file'"
	erase "`file'"

cd "C:\Users \Data"
if `j'>0 merge 1:1 _n using "Finalmat93_00", nogen
save "Finalmat93_00", replace	
local j=`j'+1

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index