0
votes

I run a DO in Stata cleaning my dataset and then call Python to run a script to generate some needed variables (3 columns) using:

outsheet unique_id group_1 group_2 group 3 group_4 group_5 group_6 group_7 using groups.csv, comma replace

shell python.exe group_fix.py

This creates a spreadsheet called group_fix.csv that has 4 columns unique_id, group_fix_1, group_fix_2, and group_fix 3. I would like to use the unique_id field (which is in my dataset and the Py output) to merge these datasets. So basically in Stata it will look like before I called Py plus the 3 new columns my script created so I can then continue on my DO and do the needed analysis.

Any ideas?

1
possible duplicate of Calling Python from Stata - Martijn Pieters

1 Answers

0
votes

I think what you're looking for is a tempfile. You can do something like this

sort unique_id
tempfile original
save "`original'"
clear
insheet using group_fix.csv, comma
sort unique_id
merge unique_id using "`original'"

This creates a temporary file named "original" which stores the original dataset. You then merge the dataset that was cleaned with the python script with the original data on unique_id. Also it's worth pointing out that in the commands like

 save "`original'"

The first ` is the character under the tilda; the second ' is a single quote

Hope that helps!