Announcement

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

  • Loop with spaces and quotes

    Hello,

    I have a list of terms that have spaces and quotes that I want to loop over. I have been able to loop over terms with spaces, I have also been able to loop over terms with quotes but never both spaces AND quotes together.

    For instance, I have the expression: Today is "Sunday"

    I have several expressions like this and I want a loop that will search this expression exactly "Today is "Sunday"", but spaces and loops are proving difficult. I find that I can read this expression properly into my loop but once I call on the local variable within the loop the text breaks down because of the quotes.

    Here is an example


    Code:
    clear all
    set more off
    
    local expression `" "Today is "Sunday"" "'
    
    gen text=`"Today is "Sunday" and tomorrow is "Monday"'
    gen flag=""
    
    set trace on
        foreach term in `expression'{
            replace flag=1 if strpos(text, `term' ) > 0
            }
            
    tab flag

    Any suggestions on how to loop over this expression fully?



    thank you,
    Donovan

  • #2
    So there are a few things. Since you -clear- the data and never -set obs-, your -gen text =...- command has no effect. Also, you initially create a string variable -gen flag = ""-, and then you attempt to -replace- it with numeric value 1 inside the loop. That provokes a "type mismatch" error.

    In terms of the issue of embedded quotes, etc., I think the principle to bear in mind is that ordinary quotes are treacherous and should be avoided. If any string has more than just a single pair of them, there is inherent ambiguity about their scope. So even in `"Today is "Sunday" and tomorrow is "Monday"' you are asking for a bit of trouble here. Does the quote at the end of Sunday open a new, embedded quotation, or does it close the quote that occurred before Sunday? Stata will take it to be the latter, but it is better to use compound double quotes when possible. Now, if you are importing a corpus of string data that already is populated with single quotes, then you have to do your best to deal with them. To that end, I think the following modifications of your code do what you intend:

    Code:
    clear all
    set more off
    
    local expression `" `"Today is "Sunday""'"'
    
    set obs 1
    gen text=`"Today is "Sunday" and tomorrow is "Monday""'
    gen flag=0
    
    set trace on
        foreach term in `expression'{
            replace flag=1 if strpos(text, `"`term'"' ) > 0
            }
            
    tab flag

    Comment

    Working...
    X