Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: Returning Labels based on values.

From   Nick Cox <>
To   "''" <>
Subject   st: RE: Returning Labels based on values.
Date   Tue, 25 Oct 2011 16:57:27 +0100

Yes. See -help macro- and -help extended_fcn-. These are undoubtedly key places for programmers. There are pointers in [U] and [P], so you didn't look in the right places. 

foreach v of numlist 1/4 {
	local label : label (SEASON) `v' 

     ... if SEASON == `v', subtitle("`label'") 


Note that there is no need to check whether the label has been defined:

sysuse auto
local label : label (mpg) 42

In this case, there is no label, but the macro just contains the value. However, your variable must be numeric. 


Cook, Daniel E

For programming purposes, I'm wondering if there is a way to return the a label based on the value used to specify it.

For example, consider a data set with the following values and labels for the variable SEASON:

1 Winter
2 Spring
3 Summer
4 Fall

Now suppose I have a do file and I want to create a graph for the mean temperature of each I write something like this:

foreach v of numlist 1/4 {

...create a graph if SEASON == `v'... of avg. temp.


Now - I want to label each graph using the value label of the season being specified. In other words, if the graph is for the Summer - I'd like to add the value label "Summer" as the title, caption, or perhaps within the filename. So I'm looking for something like this:

foreach v of numlist 1/4 {

local name getvaluelabel(`v', SEASON) //This function would return the label for a given value by specifying the variable or value label.

...create a graph if SEASON == `v'...title(`name')


I have looked everywhere for a way to do this but can't seem to find anything. Perhaps I am overlooking something.

*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index