Question

I have a longitudinal dataset in Stata and would like to create separate tsline graphs of each group inside a variable. So, say I have time running from 1980 to 2010 and categories 1, 2, ..., 17; I want to create seventeen separate tsline graphs, plotting the respective values of variable X from 1980 to 2010 and using the category label as a title.

I wrote a short loop that creates the tsline graphs, but I can't figure out how to add the correct title. The title should be label of the value of the categorical variable. That is, if I plot X for ID == 1, I want the title to be the label of ID == 1, if that is possible. Ideally, I would like to store a specific value label as local for each `i' in the loop. That way I could also use it also as part of the file name when exporting graphs, example given.

My code:

tsset ID Date, daily

forvalues i = 1/17 {
tsline X if ID==`i', title(??)
}
Was it helpful?

Solution

Use extended macro functions. Here is one example:

clear all
set more off

sysuse auto

forvalues i = 0/1 {
    local t : label origin `i'
    display "`t'"
    twoway scatter mpg weight if foreign == `i', title("`t'")
}

origin is the name of the value label associated with the variable foreign. You can list the names and contents of all value labels using label list.

The line local t : label origin `i' retrieves the label for the value in macro i, and assigns it to the macro t. Then you can use macro t as you wish.

See help label, help macro and help extended_fcn.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top