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
I then perform a series of operations.
Along the process, I save intermediate files by doing, for instance:
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:
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:
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
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
Along the process, I save intermediate files by doing, for instance:
Code:
save wide1_`ssp', replace
So, I wrote:
Code:
if `ssp'=="ssp170" { gen scen=1 } else if "`ssp'"=="ssp226" { gen scen=2 }
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 }
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
Comment