Reputation: 11
I am looking for the appropriate syntax for extracting the maximum value from a matrix and storing it in a local variable for a traditional Stata .do file program.
For example, in the matrix:
matrix X = (1,2,3 \4,5,6\7,8,9)
the function should return the number 9.
I have looked in the Mata manual and found reference to the colmax
, colmin
and max
functions for Mata, but I can't seem to get them to work.
Upvotes: 1
Views: 2560
Reputation: 37358
You should show attempts at code and what happened to get constructive advice on what you are doing wrong.
Consider
. mata: max((1,2\3,4))
4
. mat x = (1,2\3,4)
. mata : max(st_matrix("x"))
4
. mata: st_local("max", strofreal(max(st_matrix("x"))))
. di `max'
4
The term "local variable" is alien to Stata; a variable is a variable (column) in a dataset (only). The standard term is "local macro". Naturally, in many other languages, such an entity would be regarded as a variable.
Passing a maximum back to Stata as a numeric scalar would often be a better idea. Conversely, what you're most likely to be missing is that strictly a local macro is a string and so you must return its value as a string.
Upvotes: 1