I can't see why `touse' is set each time round the loop. I would move that outside.
An issue unresolved is why you want to tag your graphs and graph files with `touse'. `touse' will just be some tempname like __985123 which won't mean anything to you. Presumably you will want to use some permanent variable name instead.
Another puzzle is that you don't refer to `q' within the loop, except for the sorting.
I've not tried to understand the whole program, but I think this is closer to what you want.
program dokern, byable(recall) sortpreserve
version 9
syntax varlist [if] [in] , SAVing(string)
marksample touse
foreach q of local _byvars {
sort `q'
foreach i of local `varlist' {
twoway (kdensity `i') if `touse', ///
yla(, ang(h) labsize(vsmall)) ///
xlab(0(100)1000, labsize(tiny)) ///
caption("Kernel density of `i' by `touse'") ///
saving(`saving'_`i'_by`touse', replace )
graph export `saving'_`i'_by`touse'.ps, replace
}
}
end
A strategic comment is that I wouldn't write a program for something this specific. I would rather write a do file, get it working and make it more ambitious iteratively. That may just reflect my experience of what is natural for different kinds of tasks.
Nick
[email protected]
philippe van kerm
I'm not sure it this is the main or only source of problem, but your -foreach- statements are not correct:
foreach `q' of local `_byvars' {;
should be
foreach q of local _byvars {;
and
foreach i of local `varlist' { ;
should be
foreach i of local varlist { ;
Marco Petracco
> I have been trying to crack this using the manual and online help,
> but I don't seem to be able to come out on top of it. I am writing
> a short program which should take a list of variables and give me
> back a set of Kernel graphs and also save the results in a series
> of files, the "root" of which name should be provided by the user.
> However, no matter how I specify the value of the "saving" option
> (i.e. within parentheses and braces, parentheses only, braces only
> ...) I get a "syntax error" or an "option saving() required" error
> message.
> Can anybody help me spot what I am doing wrong?
>
> I copy here under the program:
>
> program dokern, byable(recall) sortpreserve
> # delimit;
> version 9;
> syntax varlist [if] [in] , SAVing(string) ;
> foreach `q' of local `_byvars' {;
> sort `q';
> marksample touse;
> foreach i of local `varlist' { ;
> twoway (kdensity `i') if `touse',
> ylabel(, angle(horizontal) labsize(vsmall)) xlabel(0(100)1000,
> labsize(tiny)) caption("Kernel density of `i' by `touse'") saving(
> `saving'_`i'_by`touse', replace ) ;
> graph export
> `saving'_`i'_by`touse'.ps, replace ;
> } ;
> };
> end;
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/