Quantcast
Channel: Statalist
Viewing all articles
Browse latest Browse all 65122

Saving foreach loop based on different criteria within a variable

$
0
0
I have a dataset with a variable called "users" - each user is a person, many of which have different amount of observations (user1 may have 10 observations, user 100, etc). I'd like to separate these different users into different files using a foreach loop.

This is what I have so far:

foreach user in {
"andy"
"chris"
"sarah"
.....
}
use "task.dta", clear (task.dta is the name of the file with all the aggregated information)
gen tool = "'user'"
drop if user != "tool"
save "user".dta

The desired output is a file for each user, for example:
sarah.dta
andy.dta
So and so forth until all users in the specified foreach loop are done.

My loop doesn't seem to be working, however. I seem to have hit a snag in that when I generate my tool variable, it always ends up being literally named "user" rather than the actual name of the person.

Viewing all articles
Browse latest Browse all 65122

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>