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

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

 From Oliver Jones To statalist@hsphsun2.harvard.edu Subject Re: st: Automating graphs with data-dependent reference lines and saving with meaningful names. Date Thu, 26 May 2011 14:47:44 +0200

```Am 26.05.2011 14:27, schrieb Maclennan, Graeme S.:
```
```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?
```
```
Hi,

First: Your code above didn't work perfectly. One gets an error with the
hist q, because q is an ambigous abbreviation.

Second: Yes you can tell Stata look up the value of q1 for person p == 1.

local answer_q1 = q1[1]
hist q1, discrete addplot(pci 0 `answer_q1' 20 `answer_q1' , legend(off)) percent

If you want you can create locals for all answers and all people by looping, i.e.

count
forval person = 1/`r(N)'{
forval i = 1/5 {
local answer_p`person'_q`i' = q`i'[`person']
}
}

Hope this helps a little bit.
Oliver

```
```
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/
```
```
--
Universität Bielefeld
Fakultät für Wirtschaftswissenschaften
Lehrstuhl für Ökonometrie und Statistik
- -
Bielefeld University
Chair of Econometrics and Statistics
- -
Raum / room:  V9-110
Tel / phone:  +49 (0)521 106 4871
---
*
*   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/
```