Announcement

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

  • How to get Stata to interpret a string as a command.

    For example, within a .do file, I'd like Stata to execute the command
    Code:
    syntax [,  estimate1(real 2) estimate2(real 2)]
    Is there any way to make part of that command a string, for example:
    Code:
    local mystring "estimate1(real 2) estimate2(real 2)]"
    syntax [,  `mystring'
    The code used above doesn't work, of course. I've looked through Stata's [P] manual but found nothing, perhaps because I don't know the technical name for this.

    Thanks in advance for your help.

  • #2
    A syntax command parses what is in local macro 0 according to whether it satisfies the specified syntax. It doesn't execute anything. It can define various local macros for later use or complain because the syntax given is illegal according to specified syntax. That's not the way the syntax command is usually introduced, as most commonly a syntax command appears early in a program and local macro 0 automatically contains whatever arguments were supplied to the command defined by the program.

    I wouldn't expect that statement to be easily comprehensible without a grasp of how to define a Stata program.

    In turn I don't understand what you expect to happen just because you mention syntax for a couple of options. A command without options often makes sense but options without a context don't make sense.

    I guess you need to back up and tell us much more about what you are trying to do and what the context is.

    Comment


    • #3
      My question is a more general one, so I'll give a better example.

      I'm looking for a way to get Stata to parse a macro included in a command.

      How can I get correct the code below

      Code:
      local ifstatement1 depvar>0
      regress depvar indepvar1 indepvar2 if  `ifstatement1'
      so that Stata parses the second line as:
      Code:
      regress depvar indepvar1 indepvar2 if depvar>0

      One simple way would be to form a -foreach- loop over a single item:
      Code:
      local ifstatement1 depvar>0
      foreach statement in `ifstatement1' {
          regress depvar indepvar1 indepvar2 if  `statement'
      }
      Is there another way to accomplish the same thing?

      Comment


      • #4
        I don't understand the problem. This works:

        Code:
        clear all
        corr2data depvar indepvar1 indepvar2, n(1000)
        local ifstatement1 depvar>0
        regress depvar indepvar1 indepvar2 if  `ifstatement1'
        Results:

        Code:
        . clear all
        
        . corr2data depvar indepvar1 indepvar2, n(1000)
        (obs 1000)
        
        . local ifstatement1 depvar>0
        
        . regress depvar indepvar1 indepvar2 if  `ifstatement1'
        
              Source |       SS       df       MS              Number of obs =     489
        -------------+------------------------------           F(  2,   486) =    0.23
               Model |  .169294221     2  .084647111           Prob > F      =  0.7934
            Residual |  177.642497   486  .365519542           R-squared     =  0.0010
        -------------+------------------------------           Adj R-squared = -0.0032
               Total |  177.811792   488  .364368425           Root MSE      =  .60458
        
        ------------------------------------------------------------------------------
              depvar |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
        -------------+----------------------------------------------------------------
           indepvar1 |  -.0131699   .0257907    -0.51   0.610    -.0638449    .0375052
           indepvar2 |  -.0118252   .0271335    -0.44   0.663    -.0651386    .0414881
               _cons |   .8146034   .0273448    29.79   0.000     .7608748     .868332
        ------------------------------------------------------------------------------
        -------------------------------------------
        Richard Williams, Notre Dame Dept of Sociology
        Stata Version: 17.0 MP (2 processor)

        EMAIL: [email protected]
        WWW: https://www3.nd.edu/~rwilliam

        Comment


        • #5
          Are you skipping back and forth between a do file and the command line? If so that could cause the local not to be local when you give the regress command.
          -------------------------------------------
          Richard Williams, Notre Dame Dept of Sociology
          Stata Version: 17.0 MP (2 processor)

          EMAIL: [email protected]
          WWW: https://www3.nd.edu/~rwilliam

          Comment


          • #6
            Originally posted by Richard Williams View Post
            Are you skipping back and forth between a do file and the command line? If so that could cause the local not to be local when you give the regress command.
            Thank you, Richard.
            Last edited by Sam Smith; 27 Nov 2014, 22:19.

            Comment


            • #7
              Richard, as you've been extremely helpful, I'm wondering if you might help me with a related example:


              I'd like the following .do file
              Code:
              //begin mystringdisplay.do
              
              clear
              local mystring ""
              local allcoefficients x1 x2 x3
              local mystring "[, observations(integer 1)"
              foreach coeff in  `allcoefficients' {
                  local mystring `mystring' `coeff'
                  local mystring `mystring'(real 2)
              }
              local mystring `mystring']
              macro dir
              di `mystring'
              //end mystringdisplay.do

              to display:
              [, observations(integer 1) x1(real 2) x2(real 2) x3(real 2)]

              But while the -macro dir- command displays _mystring correctly

              the display command on the last line gives me the error:
              unknown function ,observations()

              How can I get Stata to display command to work correctly, as shown above? In other words, how can I make Stata interpret the string literally rather than parsing the open parenthesis as the beginning of a function?

              Thanks again.

              Comment


              • #8
                You need quotation marks in the -display- command:
                Code:
                display `"`mystring'"'
                In your case, the use of ordinary double quotation marks will suffice, but in general it is safer to use compound double-quotes as shown.

                Comment


                • #9
                  Thanks, Clyde. Now how about if I use the same code but add another line:
                  //begin mystringdisplay.do clear local mystring "" local allcoefficients x1 x2 x3 local mystring "[, observations(integer 1)" foreach coeff in `allcoefficients' { local mystring `mystring' `coeff' local mystring `mystring'(real 2) } local mystring `mystring'] macro dir clear syntax `mystring' //The rest of the code, which uses the input variables specified above, would go below //end mystringdisplay.do [/code] How can I get Stata to parse the bolded line as syntax [, observations(integer 1) x1(real 2) x2(real 2) x3(real 2)] Thank you.

                  Comment


                  • #10
                    If you want

                    Code:
                     
                    syntax [, observations(integer 1) x1(real 2) x2(real 2) x3(real 2)]
                    you can just write it directly. The entire first segment of the code appears to be building that up very slowly and in another file. (The code isn't exactly equivalent, which may or may not be important.)

                    Why do that?

                    Comment


                    • #11
                      you can just write it directly.
                      Yes, I realize that I can write it directly.

                      I'm doing it this way because the number of variables (and types of variables) in the -syntax- statement will vary depending on a number of factors.

                      I've tried using an extremely long if statement and prefer to do it in this manner.

                      Comment


                      • #12
                        Sorry, but I remain unclear on what you want, but

                        There is absolutely no objection to passing a set of variables as a varlist.

                        There is no absolute need to know how many will be presented on a command line.

                        There is no absolute need to pass variables through options.

                        Many Stata commands could hardly be designed otherwise.

                        Comment

                        Working...
                        X