Announcement

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

  • How to use "using" in a syntax statement of an ado file

    I want to create a simple wrapper for use in order to open files compressed using gzip directly. (I am aware of gzipuse but it says that it does not work on windows)

    Here is what I have:

    Code:
    program define usegz
    version 15
    syntax [varlist] [if] [in] using
    
    tempfile temp_dta
    shell gzip --decompress --stdout `using' > `temp_dta'
    
    use `varlist' `if' `in' `temp_dta'
    end
    When I call
    Code:
    usegz "some/gz/dta/file.gz"
    I receive the error
    factor-variable and time-series operators not allowed
    I studied the documentation on this, but I cannot figure out why it's not working. Any ideas on what I am doing wrong? I do not want to use anything() in the syntax in order to keep the wrapper syntax fully identical to use.

  • #2
    The using keyword is apparently required, not optional. (Yes, I understand that for some Stata commands it is optional, but apparently those do not use the syntax command's "using" to parse their input. Or else they don't include optional varlists. Or something.) Lacking it, Stata tries to interpret your filename as a variable name.
    Code:
    . capture program drop usegz
    
    . program define usegz
      1. version 15
      2. syntax [varlist] [if] [in] using
      3. display `" hello, world! the local macro using is: `using' "'
      4. end
    
    .
    . usegz using "some/gz/dta/file.gz"
     hello, world! the local macro using is: using `"some/gz/dta/file.gz"'
    
    . usegz "some/gz/dta/file.gz"
    factor-variable and time-series operators not allowed
    r(101);
    Note that the result returned in the local macro using is not what you expected it would be - it includes the word "using". The following demonstrates the use of "using/" in the syntax command.
    Code:
    . capture program drop usegz
    
    . program define usegz
      1. version 15
      2. syntax [varlist] [if] [in] using/
      3. display `" hello, world! the local macro using is: `using' "'
      4. end
    
    .
    . usegz using "some/gz/dta/file.gz"
     hello, world! the local macro using is: some/gz/dta/file.gz
    
    . usegz "some/gz/dta/file.gz"
    factor-variable and time-series operators not allowed
    r(101);
    You will probably want to do something to ensure that both the expanded values of using and temp_dta are passed to the shell with quotation marks surrounding them, in case either of the paths includes spaces.

    Comment


    • #3
      Thanks for the clarification regarding `using` and `using/`. However, since I want to wrap `use` where `using` is optional I am in the situation you mention:

      I understand that for some Stata commands it is optional, but apparently those do not use the syntaxcommand's "using" to parse their input
      How does it work in this case?

      Comment


      • #4
        I found an example in the describe.ado and could adapt that. Now it works with

        Code:
        program define usegz
          version 15
          syntax [anything] [if] [in] [using/] [, clear Nolabel]
            
            tempfile temp_dta
            
            // syntax without using
            if (`"`using'"' == "") {
                shell gzip --decompress --stdout `anything' > `temp_dta'
                use `temp_dta', `clear' `nolabel'
            } 
            // syntay with using
            else {
                shell gzip --decompress --stdout `using' > `temp_dta'
                use `anything' `if' `in' using "`temp_dta'", `clear' `nolabel'
            }
        end

        Comment

        Working...
        X