Dear all

Please assist. I have multiple variables in one long large dataset, with same format that I would like to extract using the "keep" command, and for each group of variables I save/ output them in different files using one procedure. I have been doing this separately for more than 30 variables. Example data for "subst" and "self-harm" is shown below. I have other variables in the large dataset similar to ".....subst" e.g. ".....organic", ".......schizo" etc....

Is there a way of running syntax with one procedure and saving the subset stata files for each mental health condition?

From each group I run keep as follows:

"keep personid delay10 tot_mh_substYR sum_losYR_mh_subst seq_tot_mh_substYR" and save in a separate "subst" file
"keep personid delay10 tot_mh_selfharmYR sum_losYR_mh_selfharm seq_tot_mh_selfharmYR" and save in a separate "subst" file


Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input double personid float(delay10 tot_mh_substYR sum_losYR_mh_subst seq_tot_mh_substYR tot_mh_selfharmYR sum_losYR_mh_selfharm seq_tot_mh_selfharmYR)
 2337  1 .  . . .  . .
 2337  2 .  . . .  . .
 2337  4 .  . . .  . .
 2337  4 .  . . .  . .
 2337  4 .  . . .  . .
 2337  4 .  . . .  . .
 2337  5 .  . . .  . .
 2337  5 .  . . .  . .
 3172 -1 .  . . .  . .
 3172  0 5 15 1 7  8 1
 3172  0 .  . . 7  8 2
 3172  0 .  . . 7  8 3
 3172  0 .  . . .  . .
 3172  0 .  . . 7  8 4
 3172  0 .  . . 7  8 5
 3172  0 .  . . 7  8 6
 3172  1 .  . . .  . .
 3172  2 1 10 1 .  . .
 3172  3 .  . . .  . .
 3172  4 2  2 1 .  . .
 3172  4 2  2 2 .  . .
 3172  5 1  1 1 .  . .
 3172  0 5 15 2 .  . .
 3172  0 5 15 3 .  . .
 3172  0 5 15 4 .  . .
 3172  0 5 15 5 7  8 7
 3653  1 .  . . .  . .
 3653  2 .  . . .  . .
 3653  2 .  . . .  . .
 3653  3 .  . . .  . .
 3653  4 .  . . .  . .
 3653  5 .  . . .  . .
 3653  5 .  . . .  . .
 3653  6 .  . . .  . .
 5625  0 .  . . .  . .
 5625  0 1  0 1 .  . .
 5625  1 .  . . 2  6 1
 5625  1 2 34 1 .  . .
 5625  1 .  . . .  . .
 5625  1 2 34 2 2  6 2
 5625  3 1 26 1 .  . .
 5625  5 .  . . .  . .
10045  5 .  . . .  . .
11758 -1 .  . . .  . .
11758 -1 1  1 1 .  . .
11758  0 8 68 1 7 44 1
11758  0 .  . . .  . .
11758  0 .  . . 7 44 2
11758  0 8 68 2 .  . .
11758  0 .  . . 7 44 3
11758  0 8 68 3 .  . .
11758  0 8 68 4 .  . .
11758  0 .  . . .  . .
11758  0 8 68 5 7 44 4
11758  0 8 68 6 7 44 5
11758  0 .  . . 7 44 6
11758  0 8 68 7 7 44 7
11758  0 8 68 8 .  . .
11758  0 .  . . .  . .
11758  1 .  . . .  . .
11758  1 2  6 1 .  . .
11758  1 .  . . .  . .
11758  1 .  . . 4  5 2
11758  1 .  . . 4  5 1
11758  1 .  . . 4  5 3
11758  1 .  . . .  . .
11758  1 .  . . .  . .
11758  1 .  . . 4  5 4
11758  1 2  6 2 .  . .
11758  2 4 16 1 .  . .
11758  2 .  . . .  . .
11758  2 .  . . 2  3 1
11758  2 .  . . 2  3 2
11758  2 .  . . .  . .
11758  2 .  . . .  . .
11758  2 4 16 2 .  . .
11758  2 4 16 3 .  . .
11758  3 1  2 1 1  2 1
11758  2 4 16 4 .  . .
14193 -1 .  . . .  . .
14193  0 .  . . .  . .
14193  4 .  . . .  . .
17206  0 .  . . .  . .
17206  4 .  . . .  . .
17206  5 .  . . .  . .
17353  1 .  . . .  . .
19252 -1 .  . . .  . .
19252 -1 .  . . .  . .
19252  4 .  . . 1  3 1
19949 -1 .  . . .  . .
19949 -1 .  . . .  . .
19949  0 .  . . .  . .
19949  0 .  . . .  . .
19949  0 .  . . .  . .
19949  0 .  . . .  . .
19949  0 .  . . .  . .
19949  0 .  . . .  . .
19949  0 .  . . .  . .
19949  0 .  . . .  . .
19949  1 .  . . .  . .
end
label values delay10 delay
label def delay -1 "pre_1-3yr", modify
label def delay 0 "pre_0-1yr", modify
label def delay 1 "0-1 years", modify
label def delay 2 "1-2 years", modify
label def delay 3 "2-3 years", modify
label def delay 4 "3-4 years", modify
label def delay 5 "4-5 years", modify
label def delay 6 "5-6 years", modify