Announcement

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

  • Using -piece- from extended functions

    I am having trouble using piece from the extended functions. The help for extended functions lists the syntax but doesn't give any usage examples. I've also searched online for examples but haven't found much. Here's what I'm trying to do. Can someone tell me why it doesn't work?

    Code:
    version 11.2
    char hd01_vd01[one] Testing Hello Can You Hear Me
    local var hd01_vd01
    display `"`var'"'
    local var_char : char `var'[one]
    display `"`var_char'"'
    
    local var_char1 : piece 1 3 `"`var_char'"', nobreak
    display `"`var_char1'"'
    Here's the error I get:

    Code:
    . local var_char1 : piece 1 3 `"`var_char'"'
    invalid syntax
    r(198);

    Context: I want to store strings longer than 80 characters (pre-existing from American Community Survey) in the variable characteristics, since they are too long for the variable labels. I will then pull them into automated graphs to wrap around lines in the title or caption. Something like:

    Code:
    title(`"`var_char1'"' `"`var_char2'"' `"`var_char3'"')
    On a related note, I've been using the following code from an old Statalist post, I believe by Nick Cox. I don't understand the syntax of the quotes. Why "`...'" ? Maybe I need something similar to make the piece code work? Thanks.

    Code:
    local var_label "`: variable label `var''"

  • #2
    Note the -of- :

    Code:
     
     local var_char1 : piece 1 3 of `"`var_char'"', nobreak

    Comment


    • #3
      To answer your question regarding the quotes just type

      Code:
      loc piece : piece 1 3 of "foobar"
      di "`piece'"
      
      di `"`: piece 1 3 of "foobar"'"'
      I do not know if there is an official term for this, I would call it 'macro expansion on the fly'. Instead of assigning the contents of extended function piece to a (local) macro, you expand and display it right away.

      Best
      Daniel

      Comment


      • #4
        Originally posted by daniel klein View Post
        I do not know if there is an official term for this
        It's referred to as inline use of the extended macro function piece.

        Comment


        • #5
          A key principle of syntax here is simply that macro references can be nested, i.e. a macro reference can include a macro reference, and so on. In practice, 3 levels of nesting is more than you will often see, but even when you do encounter multiple nesting, it's just the same principle applied repeatedly.

          You may find it useful to remember exactly what was learned at a very early age about parentheses in elementary mathematics: When you come across something like (1 + 2 (3 x 4)), evaluate from the inside outwards. In something like this (from "an old Statalist post, I believe by Nick Cox": that narrows web search mightily)

          Code:
           
          local var_label "`: variable label `var''"
          the deepest level of reference is to local macro var. Stata should find that local macro and insert its contents and then the extended macro function looks up its variable label.

          Comment


          • #6
            Nick,

            I don't know if this is what Kevin was wondering or not, but is there a practical or functional difference between the following?

            Code:
            local var_label "`: variable label `var''"
            and

            Code:
            local var_label : variable label `var'
            As noted by Daniel and Phil above, the former is an example of inline macro expansion, which could be useful in other contexts, but in this usage (assigning the result to a macro) the outer double quotes are not required.

            Regards,
            Joe

            Comment


            • #7
              Usually the results are identical. But the second is more direct. The second won't fall over if there were double quotation marks embedded in the variable label.

              I don't remember the specific posting Kevin used as illustration. But I can't think of circumstances in which the first is preferable to the second, but the difference rarely bites.

              Comment


              • #8
                Thank you, I did indeed miss the "of". It works fine now. Thank you for the clarification on the syntax for inline use of extended macro functions.

                Comment

                Working...
                X