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

st: Looping with label define

From   "Eric A. Booth" <>
Subject   st: Looping with label define
Date   Thu, 19 Feb 2009 13:17:15 -0600


I am trying to define labels for a numeric variable (countynumber) using an existing string variable (county) so that the the countynumber will take on a value label equal to the county for that same row.

I would like to use looping with the "label define" command to complete this task. (I've been successful with using looping with the encode and replace commands to get this to work (after sorting the data), but my dataset has many variables (sorted in several different ways) that will get the same set of value labels & minor tweaks to the these value labels may occur frequently and so I'd like to be able to make those changes to one column and then apply it to all the associated variables in my dataset)

An example of what I've tried is below. When I run this code, it applies "Andrews" as the value label for all of the values in countynumber.


input str20 county countynumber
Andrews 1
Anderson 2
Austin	3
Bexar	5	
Childress	4
Denton	6

local testmacro = county[_n]

forvalues num = 1/6 {
	label define lab `num' "`testmacro'", modify

label val countynumber lab


Thanks for any help!


Eric A. Booth
Public Policy Research Institute
Texas A&M University
Office: +979.845.6754
Fax: +979.845.0249

*   For searches and help try:

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