Announcement

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

  • Changing local string based on if statement

    Dear Stata listers,

    Perhaps you have some thoughts about the following problem that I have been struggling with:

    My data comes out as multiple .csv files. Most of them need to be processed and reformatted the same way, but there are a few files that have some special reformatting requirements. I want to save all files as a .dta file, but the odd ones with special reformatting requirements, I specifically want to add a prefix to the .dta file name. This I would like to write in a way as generic as possible so that my colleagues can also use this script for their studies that use the same software.

    Let me walk you through my thought process with the following example:

    Code:
    * (1) In a global file where I set all my parameters, I indicate which are the files that need to be processed differently, for example:
    global list_oddfiles "fileB" "fileD"
    display "${list_oddfiles}"
    
    * (2) Then the idea is that I loop through all .csv files in the folder where I keep my raw data and that it compares the filename with the global list_oddfiles, if it is a match, I want to update a local that contains the new file name I am going to assign
    foreach filename in fileA fileB fileC fileD fileE { /* this will eventually simply loop through the folder where my raw data is kept */
        foreach oddfile of global list_oddfiles {
        noi display "going to compare now the file: " "`filename' " "with the odd file: " "`oddfile'"
        if "`filename'" == "`oddfile'" local filename = "ODD_" + substr("`filename'", 1, .)    
        *if substr("`filename'", 1, 4) == "file" local filename_new = "ODD_" + substr("`filename'", 1, .)    
        display "`filename_new'"
        }
    }
    What I don't quite get, is that this if statement does not work:
    Code:
    if "`filename'" == "`oddfile'" local filename = "ODD_" + substr("`filename'", 1, .)
    The problem seems to be specifically with the
    Code:
     "`filename'" == "`oddfile'"
    part, because you can see if I replace the if statement with something else (commented out): if
    Code:
    substr("`filename'", 1, 4) == "file" local filename_new = "ODD_" + substr("`filename'", 1, .)
    , I do not get the same error message of 'Too few quotes'.

    Would anyone have any thoughts about what the problem could be with the syntax?

    Thank you in advance for the help!

    Best wishes,

    Moniek

  • #2
    When defining a local or global macro, if Stata encounters a double quote as the first character, it will expect a matching double quote as the final character, and will remove both.
    Code:
    . global list_oddfiles "fileB" "fileD"
    
    . macro list list_oddfiles
    list_oddfiles:  fileB" "fileD
    So we need to surround the entire list in quotation marks, using compound double quotes to ensure correct parsing by Stata.
    Code:
    . global list_oddfiles `" "fileB" "fileD" "'
    
    . macro list list_oddfiles
    list_oddfiles:   "fileB" "fileD"
    See the output of help quotes for a more complete explanation of quotation marks in Stata.

    And note the use of macro list to see exactly what the contents of a macro are. The display command can try too hard to guess what you want to see.
    Code:
    . local z 5 + 3
    
    . display `z'
    8
    
    . macro list _z
    _z:             5 + 3

    Comment


    • #3
      Dear William,

      Thank you for explaining, I've read up about help quotes in the meantime, and was able to get the code to work with your help.

      Wishing you a lovely evening (or any part of the day really depending on the time zone you are in)

      Best wishes,

      Moniek

      Comment

      Working...
      X