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:
What I don't quite get, is that this if statement does not work:
The problem seems to be specifically with the
part, because you can see if I replace the if statement with something else (commented out): if
, 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
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'"
}
}
Code:
if "`filename'" == "`oddfile'" local filename = "ODD_" + substr("`filename'", 1, .)
Code:
"`filename'" == "`oddfile'"
Code:
substr("`filename'", 1, 4) == "file" local filename_new = "ODD_" + substr("`filename'", 1, .)
Would anyone have any thoughts about what the problem could be with the syntax?
Thank you in advance for the help!
Best wishes,
Moniek

Comment