I'd like to merge (by a key variable) a Mata column vector to a Stata data file. I'd like to do this without using the frames of v. 16 so as to keep backward compatibility. I thought this task would be something common and already solved by someone else, but I'm not finding anything that seems on target to me. (Perhaps my oversight.) The situation that motivates this occurs if some calculation (perhaps an estimation) is done in Mata that produces a column vector that is a new variable to be put back onto the Stata file for some subset of observations.
I could do this by preserve/clearing the Stata file, storing the Mata vector in Stata and saving it to a temp file, restoring the original file, and then -merge- ing in Stata. That's pretty klugy and slow, so I'm think there must be a more direct way that has been or can be implemented. Any suggestions?
Here's some example data to work with:
I could do this by preserve/clearing the Stata file, storing the Mata vector in Stata and saving it to a temp file, restoring the original file, and then -merge- ing in Stata. That's pretty klugy and slow, so I'm think there must be a more direct way that has been or can be implemented. Any suggestions?
Here's some example data to work with:
Code:
//Data file clear set seed 1884 sysuse auto gen int id = _n // // Give a selection of the observations/vars to Mata and create a silly new variable gen byte touse = runiform() > 0.2 // not all observations happen to be relevant mata: X =st_data(., ("id", "weight", "length"), "touse") mata: newvar = X[.,2] :/ X[.,3] // weight/length // // Now merge newvar back to Stata dataset with id as a key. How?
Comment