I am using Stata 13.1 to generate diagnostic graphs for visually inspecting unbalanced time series data. Each observation is a city i in year j. I have included a sample dataset at the bottom of this post. I plan to create one small graph for each city and then combine the small graphs into one large graph. I would like to title each small graph with the associated city name, contained in a string variable.
I am sure that there is a more elegant way to do this, but here is what I have so far. (The commands can be run using the sample dataset included at the bottom of this post; I have omitted the "saving" option from the tsline command, as it is irrelevant to my question.)
The resulting graphs accurately display the data, but do not include the title. I would be grateful for any advice about changing my code or using a different approach to accomplish the goal specified above.
I am sure that there is a more elegant way to do this, but here is what I have so far. (The commands can be run using the sample dataset included at the bottom of this post; I have omitted the "saving" option from the tsline command, as it is irrelevant to my question.)
Code:
tsset panelvar year sort panelvar year mkmat panelvar if panelvar != panelvar[_n+1] sum panelvar if panelvar != panelvar[_n+1] local max = r(N) foreach city of numlist 1(1)`max' { local thisobs = panelvar[`city',1] if panelvar == `thisobs' & panelvar != panelvar[_n+1] { local name = name } tsline var1 if panelvar == panelvar[`city',1], title("`name'") }
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input int year str10 name byte var1 long panelvar 1980 "Smith city" 1 987654 1981 "Smith city" 2 987654 1982 "Smith city" 3 987654 1983 "Smith city" 4 987654 1984 "Smith city" 5 987654 1985 "Smith city" 6 987654 1981 "Jones city" 6 687593 1982 "Jones city" 5 687593 1983 "Jones city" 4 687593 1984 "Jones city" 3 687593 1985 "Jones city" 2 687593 end