Foreach loop for graphs: only changing title

#1
Hi,

I am making 15 graphs and would like to use a foreach loop. Everything works fine, but I don't know how to make the title change for each graph.

foreach var of varlist A-Z{
scatter `var' age if sex==1, title(x)
}

....where x is (eating, walking etc.) unique for each graph.

Would be nice if you could write this code once instead of 15 times.
 

bukharin

RoboStataRaptor
#2
If you've labelled the variables properly then you can just extract each variable's label into a local macro (here called graphtitle):

Code:
foreach var of varlist A-Z {
[INDENT]local graphtitle : variable label `var'
scatter `var' age if sex==1, title(`graphtitle')[/INDENT]
}
See the manual entry for macro ([P] macro) for more info on extracting labels from variables, or the User's Guide ( 18.5 Macros) for more info on macros in general.