Announcement

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

  • The function of double quotes in combination with single quotes

    Dear all,

    I have a question on a piece of code that I was able to make work, but I don't understand why.
    I looked at several sources including Stata manuals, and the manuals for Stata course nc151 (which I took a few years ago), and other discussions in this forum, but I am still confused.

    A bit of context: I am running a loop across several files whose names are identical saved for a specific piece of text.
    To loop across the files I have generated a local macro that identifies this single changing piece of text.

    this is the initial code
    Code:
    local ssps ssp170 ssp226 ssp285
    
    foreach ssp in `ssps' {
    
    insheet using "all_data_`ssp'.csv", comma
    I then perform a series of operations.
    Along the process, I save intermediate files by doing, for instance:

    Code:
    save wide1_`ssp', replace
    Then I perform more steps...until I need to insert an IF command to perform slightly different steps depending on the ssp.
    So, I wrote:

    Code:
    if `ssp'=="ssp170" {
    gen scen=1
    }
    else if "`ssp'"=="ssp226" {
    gen scen=2
    }
    My question is this.
    When the code gets to the IF statement I receive an error message, for instance it would tell me that it cannot find ssp170.

    However, the IF statement works if I encase the `ssp' in double quotes. For instance:
    Code:
     if "`ssp'"=="ssp170" {
    gen scen=1
    }
    I do not understand why that is the case.
    What are the double quotes accomplishing in this particular circumstance?
    and why when I save intermediate files it is ok to call the content of my macro by using simply the single quotes (as I wrote above: save wide1_`ssp', replace) but when I use the IF statement it requires double quotes around the single quotes?

    Many thanks for any suggestions ... if you have no time to explain but you can point me to useful material to read, that would be much appreciated too.

    Nicola

  • #2
    Originally posted by Nicola Cenacchi View Post
    What are the double quotes accomplishing in this particular circumstance?
    The if command evaluates what follows as an expression. An expression is assumed to be a numeric one unless you explicitly state otherwise, which is what you do with the double quotes.

    Edit: My statement is not a hundred percent accurate. The if command sees something like ssp170 and interprets it as referencing a variable or a scalar name. If there is such a variable or scalar, it evaluates to ssp170[1], meaning the first observation of the variable or the value of the scalar. If the variable or scalar ssp170 happened to hold a string (yes, scalars in Stata can hold strings), then your if statement would be perfectly valid. If the variable or scalar ssp170 were numeric, you would get a type mismatch error. If the variable or scalar ssp170 does not exist, Stata will tell you, which is what happened with your code. Anyway, enclosing ssp170 in double quotes allows Stata to treat it as a literal string.


    Originally posted by Nicola Cenacchi View Post
    and why when I save intermediate files it is ok to call the content of my macro by using simply the single quotes (as I wrote above: save wide1_`ssp', replace) but when I use the IF statement it requires double quotes around the single quotes?
    Because, unlike the if command, save does not evaluate what follows as an expression. Hence, there is no need for double quotes (unless the filename contains spaces, where the double quotes are used to bind).
    Last edited by daniel klein; 14 Feb 2024, 13:46.

    Comment


    • #3
      So, ssp is the iterator in your foreach loop. It is a (temporary) local macro whose value changes at each iteration of the loop. In one such iteration it takes on the value ssp170, which means that whenever, in that iteration of that loop, Stata says the sequence of characters `ssp' it will expand that to the sequence of characters ssp170 .

      So, -if `ssp' == "ssp170"- gets expanded to -if ssp170 == "ssp170"-. And perhaps here you can see the problem. You are looking at an -if- command, and in Stata syntax what follows an -if- command must be a logical expression. The logical expression is -ssp170 == "ssp170"-. Stata's parser has to figure out what the things on both sides of the == operator are. The one on the right is easy: it's a string literal. What could the thing on the left, naked ssp170, be? It's not a string literal precisely because it doesn't have any quotation marks around it. What else could it be? It's not a numeric literal, clearly. So it must be the name of something, something that can be tested for equality with something else. The only possibilities are that it is either the name of a variable or the name of a scalar. But there is no variable or scalar named ssp170, so you get the message that ssp170 cannot be found.

      By contrast, -save- expects to see the name of a file (either pre-existing or new), and ssp170, without quotes, is a legal filename.

      Added: Crossed with #2.

      Comment


      • #4
        dear Daniel and Clyde,

        thank you so much for the clarification, much appreciated.

        best,
        Nicola

        Comment

        Working...
        X