Dear Stata users,

I create variables from matrix which got from a regression, a tabulation, or any model else. I want to gsort these variables and use them in further steps, that is to say, gsortting these varaibles is essential part of my programs. However, when I import these variales through program options (gsort(namelist)), the program can not find the very variables. And another related question is: how to make the matrix be stored in memory, thus I need not to create the same matrix at each time. Below is an example. Thank you.

Code:
program define foo

syntax , GSort(namelist)

matrix foo=(1,2,3,4\2,2,3,4\5,6,7,8\1,4,6,9)
matrix rownames foo=roff1 roff2 roff3 roff4
matrix colnames foo=coff1 coff2 coff3 coff4
matrix list foo

tempname foo
matrix `foo'=foo
svmat `foo'

if "`gsort'"!="" {
 gsort "`gsort'"
}
 
end
Code:
. foo, gsort(foo2 foo3 foo4 foo1)

foo[4,4]
       coff1  coff2  coff3  coff4
roff1      1      2      3      4
roff2      2      2      3      4
roff3      5      6      7      8
roff4      1      4      6      9
number of observations will be reset to 4
Press any key to continue, or Break to abort
number of observations (_N) was 0, now 4
variable foo2 not found
r(111);