0

I'm working on my first Stata project and I have around 30 huge datasets with hundreds of variables. I want a loop that goes through each of them, keeps only the variables I am interested in and saves the reduced form data sets somewhere else. Other solutions didn't work for me 1. because many other people want to overwrite their original sources: for me it is crucial that the original data set remains unmodified and I get the output saved somewhere else to work with. 2. I guess I made a syntax mistake, because I'm relatively new.

Here's what I've produced so far

cd Y:\MAIA\Data\Data2014\

foreach file{
keep nopnltNF NumMois ptwa Qu cvwp ctwpenwp
sort nopnltNF NumMois
save `file'.dta in Y:\MAIA\MyName\test_folder}

Resulting in the error invalid syntax r(198);

3
  • foreach file doesn't fit any of the allowed syntax patterns. There is always an in or an of as keyword. Beyond that, did you read in any data before doing this? Commented Apr 10, 2017 at 12:42
  • @NickCox No, I had my test file open, but now that you mention it, of course I need to do something so that the loop opens all my data sets one after another. I think this should take care of it, however, it also gives me "invalid" as output... foreach file in wd{ use ´file' . keep nopnltNF NumMois ptwa Qu cvwp ctwpenwp . sort nopnltNF NumMois save `file'.dta in Y:\MAIA\MyName\test_folder} Commented Apr 10, 2017 at 13:24
  • The loop there does no harm but it's just a loop over one file starting with use wd Note the save command is wrong. Commented Apr 10, 2017 at 13:29

1 Answer 1

1

Here is one approach to getting a list of Stata files and looping over them.

// get a list of all Stata files in directory
loc datasets : dir . files "*.dta"

// location for transformed files
loc newplace "tmp"

// loop over each dataset
foreach f of local datasets {
    di as result "Reading `f'"
    use "`f'", clear
    keep nopnltNF NumMois ptwa Qu cvwp ctwpenwp
    sort nopnltNF NumMois
    save "`newplace'/`f'"
}

But, note that append can already handle multiple datasets, and has a keep option:

append using `datasets', keep(foo bar)

This will produce one dataset in memory.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.