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

st: Re: Looping with label define

From   "Martin Weiss" <>
To   <>
Subject   st: Re: Looping with label define
Date   Thu, 19 Feb 2009 20:33:49 +0100


_n is 1 as default, so your "testmacro" thing gets stuck in position "Andrews ". Note that _n refers to a position in the dataset, which you do not change in the loop...

----- Original Message ----- From: "Eric A. Booth" <>
To: <>
Sent: Thursday, February 19, 2009 8:17 PM
Subject: st: Looping with label define


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:

*   For searches and help try:

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