Announcement

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

  • Compound Double Quotations

    I have string variables in my data set. One of the entries has its own set of quotations.

    So when I tell stata:

    HTML Code:
    replace var = 43 if ReportType == "Dog treats - Dog treats found at facility (not "Cat Treats") - SIL"
    The portion that says "Cat Treats" is getting blacked out, rather than red (and therefore included in the full quotations).

    When I put single quotations around the entire phrase:

    HTML Code:
    replace var = 43 if ReportType == '"Dog treats - Dog treats found at facility (not "Cat Treats") - SIL"'
    Nothing happens.

    What should I do here?

    Thanks,

  • #2
    Code:
    help quotes

    Comment


    • #3
      I have a side suggestion here, not tied to the use of quotes: I would caution against using a condition that checks for the *exact* contents of a long-ish string. This is a pretty brittle condition, and might be thrown off by the presence of spaces or some other minor variation. In situations like this, I would just check for the presence of some substring, e.g., -if strpos(ReportType, "Dog treats") > 0- I'd also make my comparision with the case of the variable and the search string forced to upper or lower, e.g.
      -if strpos(lower(ReportType), "dog treats") >0- There are, of course, situations in which the exact contents of the whole string are what is needed, but in my experience those situations are rare.
      Last edited by Mike Lacy; 22 Feb 2023, 14:44. Reason: Corrected use of "substr" where "strpos" was needed.

      Comment


      • #4
        Let me specifically address the typographical error in post #1. You are on the right track with compound double quotes.

        But those are not to be single quotations around double quotations. They are
        Code:
        `"example"'
        Of the two outermost characters here, both in red, the first character is (on an American English keyboard) the "left single quote" ("accent grave") beneath the tilde (~) character below the ESC key, and the final character is the usual "single quote" ("apostrophe") just to the left of the RETURN key.

        You are perhaps new to Stata, and if so, keep this in mind when you encounter local macros, which are also surrounded by the accent grave and apostrophe.

        Also, a warning in advance about Stata's documentation. StataCorp uses – in their PDFs and in the Stata Journal – the typographical left and right "single quotes"
        Code:
        ā€˜v’ 
        rather than the "accent grave" and "apostrophe" characters
        Code:
        `v'
        users must type for Stata to recognize them. So that's another way to get confused.

        Comment

        Working...
        X