Hi all
My goal is to calculate an index using mata for multiple data files (200+ cities) in the working directory, "d:/cities". I have to use mata because some matrix operations are fast for my analysis. I calculate an index using mata, and save it to a temp file. The following code works for the one data file (one city):
************************************************** ************************************************** *****
clear all
infile str4 cityname jobs latitude longitude using "d:/cities/nyc.txt", clear
drop if jobs==0
mata:
jobs = st_data(.,"jobs")
n = length(jobs)
d = J(n,n,.) // Initiate the pair-wise distance matrix
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++) {
d[i,j]=exp(-sqrt((longitude [i]-longitude [j])^2+(latitude [i]-latitude [j])^2))
}
}
index = cross(cross(jobs,d)',jobs)/sum(jobs)/sum(jobs)
st_numscalar("myindex", index)
end
local myindex=index
tempname memhold
postfile `memhold' str4 cityname myindex using "d:\cities\output\index_out.dta", replace
post `memhold' (cityname[1]) (`myindex')
postclose `memhold'
************************************************** ************************************************** *****
But if I use a foreach loop to repeat the above calculation for many cities, the code does not go through, but stops with an error message, -- break --
************************************************** ************************************************** *****
clear all
cd "d:/cities"
fs *.txt // Get the list of the file names
foreach f in `r(files)' {
infile str4 cityname jobs latitude longitude using "d:/cities/`f'", clear
mata:
jobs = st_data(.,"jobs")
n = length(jobs)
d = J(n,n,.) // Initiate the pair-wise distance matrix
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++) {
d[i,j]=exp(-sqrt((longitude [i]-longitude [j])^2+(latitude [i]-latitude [j])^2))
}
}
index = cross(cross(jobs,d)',jobs)/sum(jobs)/sum(jobs) // This index is a scalar.
st_numscalar("myindex", index)
end
local myindex=index
tempname memhold
postfile `memhold' str4 cityname myindex using "d:\cities\output\index_out.dta", replace
post `memhold' (cityname[1]) (`myindex')
}
postclose `memhold'
************************************************** ************************************************** *****
I expected `myindex' values are stored in the "d:\cities\output\index_out.dta" for each city, but the program does not run. I would really appreciate it if someone could provide some help to tackle the problem.
Sincerely,
Bill K.
My goal is to calculate an index using mata for multiple data files (200+ cities) in the working directory, "d:/cities". I have to use mata because some matrix operations are fast for my analysis. I calculate an index using mata, and save it to a temp file. The following code works for the one data file (one city):
************************************************** ************************************************** *****
clear all
infile str4 cityname jobs latitude longitude using "d:/cities/nyc.txt", clear
drop if jobs==0
mata:
jobs = st_data(.,"jobs")
n = length(jobs)
d = J(n,n,.) // Initiate the pair-wise distance matrix
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++) {
d[i,j]=exp(-sqrt((longitude [i]-longitude [j])^2+(latitude [i]-latitude [j])^2))
}
}
index = cross(cross(jobs,d)',jobs)/sum(jobs)/sum(jobs)
st_numscalar("myindex", index)
end
local myindex=index
tempname memhold
postfile `memhold' str4 cityname myindex using "d:\cities\output\index_out.dta", replace
post `memhold' (cityname[1]) (`myindex')
postclose `memhold'
************************************************** ************************************************** *****
But if I use a foreach loop to repeat the above calculation for many cities, the code does not go through, but stops with an error message, -- break --
************************************************** ************************************************** *****
clear all
cd "d:/cities"
fs *.txt // Get the list of the file names
foreach f in `r(files)' {
infile str4 cityname jobs latitude longitude using "d:/cities/`f'", clear
mata:
jobs = st_data(.,"jobs")
n = length(jobs)
d = J(n,n,.) // Initiate the pair-wise distance matrix
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++) {
d[i,j]=exp(-sqrt((longitude [i]-longitude [j])^2+(latitude [i]-latitude [j])^2))
}
}
index = cross(cross(jobs,d)',jobs)/sum(jobs)/sum(jobs) // This index is a scalar.
st_numscalar("myindex", index)
end
local myindex=index
tempname memhold
postfile `memhold' str4 cityname myindex using "d:\cities\output\index_out.dta", replace
post `memhold' (cityname[1]) (`myindex')
}
postclose `memhold'
************************************************** ************************************************** *****
I expected `myindex' values are stored in the "d:\cities\output\index_out.dta" for each city, but the program does not run. I would really appreciate it if someone could provide some help to tackle the problem.
Sincerely,
Bill K.
Comment