Error in Stata: add new variable in Mata


Stata logo med blue.png


One example of error in Mata is when executing spgen command, i.e. creating spatially lagged variables. In this case, variable name is netwealth_riil_hhadj_avg

spgen netwealth_riil_hhadj_avg, lon(longitude) lat(latitude) swm(bin) dist(200) dunit(km)

will give error as below.

splag1_netwealth_riil_hhadj_avg_b invalid name
             st_addvar():  3300  argument out of range
             calcsplag():     -  function returned error
                 <istmt>:     -  function returned error

This error must be read in a reversed order (traceback log), i.e. from bottom to top.

The problem is when we type the command (the <istmt>), it called the calcsplag(), and calcsplag() called st_addvar(). At this point, it went wrong.

The wrong thing occurs when Mata want to execute st_addvar().

From the Mata manual reference, the st_addvar()refers to Mata’s attempt to add new variable in an existing dataset.

Attempt to create a new variable splag1_netwealth_riil_hhadj_avg_b is unsuccessful. Mata recognized this name as invalid. 

Then, by looking at Mata manual reference regarding variable naming procedure,  Mata accepts variable name between 1-32 characters. On the other hands, the new variable splag1_netwealth_riil_hhadj_avg_b contains 33 characters.

The solution is rename with the shorter character length, or duplicate the variable and naming it with a shortened name.

rename netwealth_riil_hhadj_avg nw_a

or

. gen nw_a = netwealth_riil_hhadj_avg
(6,785 missing values generated)

Then execute spgen command

. spgen nw_a, lon(longitude) lat(latitude) swm(bin) dist(200) dunit(km) nostd
Size of spatial weight matrix:      284
Calculating bilateral distance...
Calculating spatial weight matrix...

Distance by Vincenty formula (unit: km)
splag1_nw_a_b is generated in the dataset.

Source:


(Thomas Soseco)


Popular posts from this blog

Skewness dan Kurtosis

Piramida Distribusi Kekayaan Masyarakat Indonesia

KKN di Desa Penari

Palma Ratio Indonesia

Daya Beli Masyarakat, in this Economy: Dunia Usaha dan Perspektif Ekonomi Makro

Generasi Hutang: Literasi Keuangan dan Kekayaan Rumah Tangga

Berapa Rata-Rata Kekayaan Rumah Tangga di Indonesia?