Hello,
I'm working with a dataset of 32,399 observations and 4 variables in Stata 17 on Windows 10.
I'd like to use a loop to produce, name, and save multiple graphs. I found this post: Using for loop to plot multiple graphs. - Statalist, but wasn't able to work out how to modify the code for use with my data.
My data are long with: 73 unique patient identifiers (PID); between 1 and 3 visits per patient; a time variable that runs from 0-180 minutes for each PID-visit combination; and a variable Y where each value is a measure of the variable at each of the 180 minutes. I hope the dataex below is sufficient, I was worried about including too many observations and making this post very unwieldy.
I tried a nested loop with:
This gave me

When what I am after is the following for each PID at each visit, with the title being the PID and visit number:

Any advice would be greatly appreciated! Thanks very much.
I'm working with a dataset of 32,399 observations and 4 variables in Stata 17 on Windows 10.
I'd like to use a loop to produce, name, and save multiple graphs. I found this post: Using for loop to plot multiple graphs. - Statalist, but wasn't able to work out how to modify the code for use with my data.
My data are long with: 73 unique patient identifiers (PID); between 1 and 3 visits per patient; a time variable that runs from 0-180 minutes for each PID-visit combination; and a variable Y where each value is a measure of the variable at each of the 180 minutes. I hope the dataex below is sufficient, I was worried about including too many observations and making this post very unwieldy.
Code:
* Example generated by -dataex-. For more info, type help dataex clear input float(PID visit time) double Y 4472 1 0 22.548 4472 1 1 28.894 4472 1 2 35.73 4472 1 3 38.848 4472 1 4 40.612 4472 1 5 41.891 4472 1 6 43.003 4472 1 7 44.068 4472 1 8 45.127 4472 1 9 46.198 4472 1 10 47.285 4472 1 11 44.999 4472 1 12 42.425 4472 1 13 42.182 4472 1 14 42.8 4472 1 15 43.738 4472 1 16 44.797 4472 1 17 45.904 4472 1 18 47.033 4472 1 19 48.173 4472 1 20 49.321 4472 1 21 53.73 4472 1 22 58.438 4472 1 23 60.935 4472 1 24 62.632 4472 1 25 64.046 4472 1 26 65.368 4472 1 27 66.668 4472 1 28 67.972 4472 1 29 69.289 4472 1 30 70.624 4472 1 31 68.578 4472 1 32 66.244 4472 1 33 66.243 4472 1 34 67.103 4472 1 35 68.282 4472 1 36 69.581 4472 1 37 70.928 4472 1 38 72.294 4472 1 39 73.67 4472 1 40 75.053 4472 1 41 76.442 4472 1 42 77.834 4472 1 43 79.231 4472 1 44 80.633 4472 1 45 82.038 4472 1 46 83.449 4472 1 47 84.863 4472 1 48 86.281 4472 1 49 87.704 end
I tried a nested loop with:
Code:
foreach i of varlist PID { foreach j in visit { twoway line Y time if PID == `i', ytitle(Y) title(`i' `j') graph save graph_`i'_`j', replace } }
This gave me
When what I am after is the following for each PID at each visit, with the title being the PID and visit number:
Any advice would be greatly appreciated! Thanks very much.
Comment