Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# st: Automating graphs with data-dependent reference lines and saving with meaningful names.

 From "Maclennan, Graeme S." To "'statalist@hsphsun2.harvard.edu'" Subject st: Automating graphs with data-dependent reference lines and saving with meaningful names. Date Thu, 26 May 2011 13:27:01 +0100

```Dear Statlist, I have a tricky (at least to me) problem which I am currently unable to solve.  I have P persons that answer Q Likert style questions on a scale of 1 to 9.  What I need is plot of the distribution of responses to each question, but for each person P I want to indicate (using a line say) on the graph their response. This is to be fedback at a later stage, so I want to save each graph with a useful name that indicates the P and Q.   So in "pseudo code":

For each qestion Qj
For each person Pi
Histrogram Qj, discrete add line at  value of Pi response to Qj, Save graph with name PiQj

Problem 1 is getting the line to be different for each person P, in the archives I found that -addplot- with -pci- gives me what I want, but I cannot work out to automate the reading of the x-value. For example in the baby dataset below

******** Example Dataset************
clear
set obs 20
set seed 1234
gen p = _n
forvalues j = 1/5{
gen q`j' = 1 + int(9*uniform())
}

hist q, discrete addplot(pci 0 5 20 5 , legend(off)) percent
***************End********************

P1 answered 5 to Q1, but is it possible to tell Stata to read the value automatically?

Problem 2 is getting trying to loop over each question for each person and save the graphs.  In reality I wont have nice IDs for P, I have been trying to combine -foreach- over Q and levels of P but I know I am wrong.
Plus I need to incorporate the solution to above Problem 1 into the loop.  Ignoring that I have been trying variations of the below, but I am out of my depth.

******** Attempt**********
levelsof p, local(pi)
foreach q in q1-q4{
foreach i of local pi{
hist q, discrete addplot(pci 0 5 20 5 , legend(off)) percent saving(`q'`p')    /// In here I need to somehow incorprate solution to Problem 1.
}
}
**********End Attempt******

Any pointing in the right direction is appreciated.
Regards,
Graeme MacLennan

The University of Aberdeen is a charity registered in Scotland, No SC013683.

*
*   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/
```