2

I want to save the results of my Stata forvalues loop into individual files. One component of the filename should be the value j assigned to the macro within a forvalues loop.

Apparently my code leads to an instruction always to save with 1995. As such, I get messages telling me that this file already exists.

I am using the following code:

local j = 1995

forvalues `j'= 1995 / 2012 {  
    clear  
    use "/Users/carl/Desktop/STATA/Neustart/eventdates.dta", clear  
    keep if eventyear == `j'  
    sort acq_cusip eventdate  
    compress  
    save "/Users/carl/Desktop/STATA/Neustart/eventdates_`j'.dta"  
}

Does anyone have an answer to that?

2 Answers2

1

use replace

save "/Users/carl/Desktop/STATA/Neustart/eventdates_`j'.dta",replace

Updated

cd  "C:\Users\Vista\Desktop\Stataproject"


 forvalues j=1/5 {  
 sysuse auto,clear
      keep if rep78== `j'   
       save "auto_`j'.dta",replace
   }

Example with auto data in Stata . For details please see, Speaking Stata: How to face lists with fortitude

Metrics
  • 15,172
  • 7
  • 54
  • 83
1

In your original code Stata sees `j' inside the forvalues command (instead of the correct j), so it starts to evaluate that before it starts the loop. So what is eventually run is

forvalues 1995=1995/2012 {

This means that forvalues is changing the content of the local macro confusingly but legally called `1995' to 1995 in the first iteration, 1996 in the second iteration, etc. So when you refer to the local `j' inside the loop, it will not have changed and remains at the original value which you defined before the loop.

The way to solve this is to replace:

local j = 1995
forvalues `j' = 1995/2012 {

with:

forvalues j = 1995/2012 {
Nick Cox
  • 35,529
  • 6
  • 31
  • 47
Maarten Buis
  • 2,684
  • 12
  • 17
  • he tells me "j was not found" then, maybe because if i kick out the local there exists no definition for j so that stata doesnt know it – user2576879 Jul 12 '13 at 15:39
  • you should only remove the quotes within the `forvalues` command, but leave them everywhere else – Maarten Buis Jul 12 '13 at 15:42
  • can you maybe also tell me why within the loop the `j' expression does refer to the normal variable j and not to a local macro? Because I always thought that `' would only be used to call upon macros. – user2576879 Jul 12 '13 at 15:50
  • The error message "j was not found" refers to the variable j not being found not to the local macro \`j' not being found. So the problem was **not** that the local macro was not difined, in fact it was defined by `forvalues`, but that you failed to evaluate the local macro by removing the quotes, probably by typing `keep if eventyear== j`, `j` without quotes means the variable j not the local macro \`j`. – Maarten Buis Jul 12 '13 at 15:51
  • The trick is to know that Stata will evaluate macros as soon as it sees them and before it will execute commands. So you need to think about what you want Stata to see. With `forvalues` you want it to see the name of the new local macro, so you use j without quotes, but inside the loop you want Stata to see the content of the local macro j, so you use quotes. – Maarten Buis Jul 12 '13 at 15:54