Stata and StatTransfer
Wednesday, 17 June 2009
If you collaborate with people who use other statistical software with proprietary data file formats, you will find Stat/Transfer useful for converting between Stata and said software. How to do that using the Stat/Transfer GUI is easy enough to figure out. But you can also run Stat/Transfer from within Stata. If you are on a Windows machine, here's one example:
local from "D:\original files" local to "D:\sas files"// add file names here as needed local txt_files "BPALL" local xls_files "BO"// transfer to SAS format. see Stat/Transfer command file help // sasx is for SAS Transport files. Change as needed on line 35 below. Options are: /* SAS V6 for Windows and OS/2 -- sas2 SAS V7, V8 and V9 for Windows -- sas SAS V6 for Mac, Unix - HP, Sun, IBM -- sas1 SAS V6 for Unix - DEC -- sas4 SAS Transport Files -- sasx */ // other options: // /Y means "yes, overwrite as needed" // /V means "verbose output" tempname fh_out local foo set path="C:\Program Files\StatTransfer7\";%path% local myfileout "`from'\convert.bat" file open `fh_out' using "`myfileout'", replace write file write `fh_out' `"`foo'"' _n foreach filetype in txt xls { foreach filename in ``filetype'_files' { local input_file `from'\\`filename'.`filetype' local output_sas `to'\\`filename'.dat capture confirm file "`output_sas'" if _rc!=0 { local bar st "`input_file'" sasx "`output_sas'" /Y /V file write `fh_out' `"`bar'"' _n } } } file close `fh_out' winexec "`myfileout'"
There you have it. This works with Stata 9.2 and Stat/Transfer 7. I don't have either. I wrote this for a client and tested in on their machine.
Update: still unclear why indentation isn't working like it used to, but there's a workaround: using the <pre></pre> HTML tags. Having a graphical interface like WordPress for blogging should in theory make it unnecessary to revert to HTML, but it's only one pair of tags and I like the result. I'll live with it. As to a non-Windows version of the code above, check Eric's comment below.
No. 1 — June 18th, 2009 at 8:16 pm
Nice code...very helpful.
As far as other platforms go, your code should work if you change the filepaths (see my comment in your previous posting) and you convert the -winexec- command to -!- or -shell-.
I have a similar job set up, but with a slightly different purpose. I've got a task set up to periodically convert files copied over from our server using -stcmd- (above you have just -st-...when I try just -st- Stata doesnt recognize the stat/transfer command (?)). In a non-windows environment, you should really probably set up a cron job (versus the batch/.bat file used in windows) to perform this kind of periodic work, but I cheat and put the command
Stata -b do dofile.do
in MacOSX's "iCal.app" as an event on the calendar, and tell it to run weekly on Friday at 6pm.
In "dofile.do" I put a loop containing the files to copied and converted using -stcmd-, so something like:
*******
local in //server/serverpath/folder/"
local out "/users/username/folder/"
forvalues n = 1/1000 {
tokenize `" "file`n'" "newfile`n'" "report`n'" "anotherfile`n'" "'
while "`1'" != "" {
stcmd "`in'`1'.csv" "`out'`1'_converted`c(current__date)'.dta" /y /q /od
cap mkdir "`in'backup/`c(current_date)'/", public
copy "`in'`1'.csv" "`in'backup/`c(current_date)'/`1'.csv", replace public
mac shift
}
*********
This should take all 1000 of each of the 4 file types (file, newfile, report, anotherfile) and convert them from .csv to .dta (or whatever file types you want...I just used these as an example here), put them in the new directory (out) with the current date in the folder name, and then save a copy of the .csv file in the backup directory. Note that I put the current date in the file name and the backup folder name because I run this job weekly (sometimes more) and I don't want to hassle with finding the version of the dataset I need.
No. 2 — June 18th, 2009 at 8:17 pm
There are probably several errors in the example code I put in my previous post, but here's one I noticed just as I hit the 'submit' button,,,,the local macro for the `in' directory needs an opening quote:
local in "//server/serverpath/folder/"
No. 3 — June 21st, 2009 at 1:18 pm
I think the difference between st and stcmd is that the former implies the Stat/Transfer command COPY, while the latter requires an explicit command. So "st" and "stcmd copy" should be equivalent.