Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • "no variables defined" in program

    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:

    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)
    My an example of an adofile for the merge is:

    Code:
    cap prog drop mergerprogram
    prog define mergerprogram
    
        syntax, idvar(varname)
        
        use data1, clear
            
            merge 1:1 `idvar' using data2
    
    end
    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":

    Code:
    sh del temp\*.dta
    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

  • #2
    You're having this problem because, at the time you execute the -syntax- command in -mergerprogram.ado-, there are no variables in the data in memory. And option -idvar(varname)- expects the argument to match the name of a variable in current memory, not in some file that might be read in later.

    The solution is to change the -syntax- command to -syntax, idvar(name)-. Now -syntax- will just expect the argument of -idvar()- to be a legal Stata name.

    As for your second minor question, you have this problem because /* is the way to open a block comment in Stata. You can override that, in this context, by changing it to:
    Code:
    sh del "temp/*.dta"
    Binding the path in quotes will prevent Stata from recognizing the /* as the start of a block comment, and won't interfere with the interpretation of shell or del.

    Added: I gather you expect your mergerprogram.ado to be of some general use. Perhaps what you have shown is just an very stripped-down version to make your point here without drowning us in code that is mostly not relevant. But I would think that a general program whose job is to merge a bunch of files would support -merge- keys with more than just one variable. If your real program needs to do that, then it would be -syntax, idvar(namelist)-.
    Last edited by Clyde Schechter; 16 Feb 2024, 10:47.

    Comment


    • #3
      Thank you Clyde, your solution worked perfectly!

      Comment

      Working...
      X