Hello!
I'm writing a program that calls several ado files. The job of the first ado file is to merge datasets. This is a program that will be distributed around other researchers, so that a similar analysis can be performed by all, thus, I do not know what are the variable names in the datasets that will be merged.
My problem arises from, as I start without any datasets loaded into memory (loading datasets into memory only happens when it is time to merge them), Stata returns the error "no variables defined".
A mock example of my main dofile, with the generation of two example datasets, is here:
My an example of an adofile for the merge is:
If if would like to run it, this code should be saved as an adofile called mergerprogram.ado, in the folder specified on "adopath ++".
Running this cod as is thus returns the mentioned error.
Uncommenting "gen id = . " in the main dofile solves the problem, but requires me knowing before hand what the variable will be called. Loading one of the datasets into memory before hand also wouldn't work because in the mergerprogram, other variables such as turnover and region are specified, and won't be present in the dataset I would load, as all variables being present in a single dataset will only happens after the merge.
An additional minor question is with regards using "" or "/". I was told that "/" was better because of potentially distributing the code to non-windows users. However, in one of my .ado files, I use the following code to delete all dta files in a folder called "temp":
Here, if I use a forward slash, the remaining of the code becomes commented. Is there a solution around this?
Thanks a lot for your help.
Best,
Hélder
I'm writing a program that calls several ado files. The job of the first ado file is to merge datasets. This is a program that will be distributed around other researchers, so that a similar analysis can be performed by all, thus, I do not know what are the variable names in the datasets that will be merged.
My problem arises from, as I start without any datasets loaded into memory (loading datasets into memory only happens when it is time to merge them), Stata returns the error "no variables defined".
A mock example of my main dofile, with the generation of two example datasets, is here:
Code:
// generate test data clear all cd "C:/Users/helder.ascosta/Desktop/test_folder" set obs 10 gen id = _n save data1, replace clear set obs 10 gen id = _n save data2, replace //start of the test main dofile clear all cd "C:/Users/helder.ascosta/Desktop/test_folder" adopath ++ "C:/Users/helder.ascosta/Desktop/test_folder" //gen id = . mergerprogram, idvar(id)
Code:
cap prog drop mergerprogram prog define mergerprogram syntax, idvar(varname) use data1, clear merge 1:1 `idvar' using data2 end
Running this cod as is thus returns the mentioned error.
Uncommenting "gen id = . " in the main dofile solves the problem, but requires me knowing before hand what the variable will be called. Loading one of the datasets into memory before hand also wouldn't work because in the mergerprogram, other variables such as turnover and region are specified, and won't be present in the dataset I would load, as all variables being present in a single dataset will only happens after the merge.
An additional minor question is with regards using "" or "/". I was told that "/" was better because of potentially distributing the code to non-windows users. However, in one of my .ado files, I use the following code to delete all dta files in a folder called "temp":
Code:
sh del temp\*.dta
Thanks a lot for your help.
Best,
Hélder
Comment