Announcement

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

  • Is there a Stata macro equivalent to argc of C?

    I would like to know the most efficient method (or even the least efficient, but working method!) to return a string when a program does not receive any arguments but expects some.

    For example, I have a small program called "twobytwo" that takes two arguments - lead and gold.
    How do I get stata to return "usage: twobytwo lead gold" if I just type twobytwo with no arguments?

    I have tried testing "`1'" for null value, to no avail.

    I don't find anything akin to argc of C

    any suggestions would be welcome.

    Thanks

    Joe

  • #2
    There are two method, actually. You can look at -args- or -syntax-. I recommend opening the PDF documentation and reading Chapter 18 Progamming Stata of the [U] User's Guide. In particular, pay attention to Section 18.4 Program Arguments.

    Comment


    • #3
      Originally posted by Joseph Miller View Post
      I have tried testing "`1'" for null value, to no avail.
      Code, please!

      Code:
      if "`1'" == ""
      should do the trick.

      Comment


      • #4
        Daniel, thank you for your interest in my problem.


        3. if "`1'" == "" di "usage: twobytwo lead gold"

        . // end of file twobytwo
        .

        produces the following:

        . twobytwo
        ==1 invalid name
        r(198);

        hence, the question. My supposition is that if there is no first argument, there is no macro assignment of 1
        This kind of makes sense because otherwise a null macro up to the maximum number of arguments would need to be generated

        any other suggestions? I tried Sizeof() but got similar results.

        73 de KI7WV
        Joe

        Comment


        • #5
          Originally posted by Joseph Miller View Post
          any other suggestions?
          Maybe something along the following lines.
          Code:
          version 17.0
          
          clear *
          
          *
          * Begin here
          *
          program define twobytwo
              version 17.0
              syntax [anything(name=leadgold)]
          
              local tally : word count `leadgold'
          
              if `tally' != 2 {
                  display in smcl as error "usage: twobytwo lead gold"
                  exit = 198
              }
              else {
                  // No op
              }
          
              gettoken lead gold : leadgold
              display in smcl as result stritrim("`lead' and `gold'")
          end
          
          // Typical use cases
          twobytwo 0 1
          
          twobytwo up down
          
          // Error trapping
          twobytwo
          
          exit

          Comment


          • #6
            I have tried testing "`1'" for null value, to no avail.
            `' isn't a way to dereference a pointer in Stata, at least not in the C sense, and the surrounding quotes convert the macro into a string anyway. Ado usually denotes an unset or missing value with the missing symbol (.) and the empty macro as the empty string. As far as I know, Ado doesn't have a null type. Unless you are using Mata?

            Regardless, I think Leonardo is correct in #2. Taking advantage of the more advanced augment parsing system with args or syntax is the way to go here.
            Last edited by Daniel Schaefer; 13 Mar 2023, 21:58.

            Comment


            • #7
              Originally posted by Joseph Miller View Post
              3. if "`1'" == "" di "usage: twobytwo lead gold"
              produces the following:

              . twobytwo
              ==1 invalid name
              r(198);
              I cannot replicate this problem:

              Code:
              . program twobytwo
                1.    
              .     if "`1'" == "" display "usage: usage: twobytwo lead gold"
                2.    
              . end
              
              .
              . twobytwo
              usage: usage: twobytwo lead gold
              Anyway, here are a couple of alternatives, testing slightly different conditions.

              Code:
              program twobytwo
                  
                  if "`0'" == "" display "usage: usage: twobytwo lead gold"
                  
              end
              
              program twobytwo
                  
                  capture confirm existence `0'
                  if ( _rc ) display "usage: usage: twobytwo lead gold"
                  
              end
              
              program twobytwo
                  
                  syntax varlist(min = 2 max = 2)
                  
              end
              Last edited by daniel klein; 14 Mar 2023, 01:50.

              Comment


              • #8
                Thanks everyone
                Joe

                Comment


                • #9
                  PS - I am running Stata 12 - perhaps this is the issue.
                  Joe

                  Comment


                  • #10
                    Originally posted by Joseph Miller View Post
                    PS - I am running Stata 12 - perhaps this is the issue.
                    I do not think it is relevant to this specific scenario because the involved elements (args, syntax, local macros 0, 1, ...) have been around much longer. However, it is always a good idea to tell us the version of Stata that you are using.

                    Comment


                    • #11
                      version

                      capture program drop twobytwo

                      program define twobytwo
                      args lead gold
                      if "`0'" == "" display "usage: twobytwo lead gold"
                      display "`lead' `gold'"
                      end


                      works as expected. I must have had some typo not recognized.
                      I have no idea why 0 is a null, but this is a good hack.
                      fwiw, having read he man pages, I don't see a way of providing this type of usage prompt more efficiently. Thank you Daniel!
                      closing thread with great appreciation.
                      Joe

                      Comment


                      • #12
                        I believe the traditional pattern for testing for the presence of a macro variable in many languages is:

                        Code:
                        if "`1'x"!="x" {
                        and it works in Stata also.

                        Comment


                        • #13
                          The common pattern I see, even from Stata developers, to test for the presence of an argument is:

                          Code:
                          if “`x’”==“x” {
                          this just saves typing a few characters is all.

                          Comment

                          Working...
                          X