Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: labels as values of a string variable

From   Nick Winter <>
Subject   Re: st: labels as values of a string variable
Date   Thu, 02 Jun 2005 10:08:31 -0400

I haven't been following this thread, so I don't know why you are trying to do this; I suspect htere is a better way.

That said, what's going on here is this. The line:

local label`i' = "string[_n]"

is assigning the literal string 'string[_n]' to the local macro label1

But, when you say

di `label1'

Stata interpets that as

di string[_n]

and then it displays the contents of string[_n] -- that is, it displays the first observation of the variable string.

You will see this if you instead type

di "`label1'"

which will display the text


What you need in your original line is this:

local label`i' = "`=string[_n]'"

In other words, you wrap it in the double quotation marks, to deal with the spaces. But inside the quotes, you surround it with `= and ' , which tell stata to evaluate the expression. This gives you the contents of string[_n], rather than the literal string "string[_n]"

All of that said, unless I missed something, the _n in this expression is the equivalent of a 1 -- that is, you are just grabbing the first observation of each.

Would -encode- do what you want? Or at least your first approach would work as:

levels string, local(labels)
tokenize `"`labels'"'

What is your ultimate goal?

--Nick Winter

At 09:41 AM 6/2/2005, you wrote:


Thanks for your suggestion. Unfortunately it didn't work. The value that I am trying to assign is a string containing space, e.g. "Gross foreign direct investment (% of GDP)". Therefore I have to include the quotation marks (""). In fact the coding I send in my previous email was assigning the string values to the local macros, the problem was when I tried to label the variables using those local macros.



----- Original Message ----- From: "James Cui" <>
To: <>
Sent: Thursday, June 02, 2005 3:25 PM
Subject: Re: st: labels as values of a string variable

If you change
local label`i' = "string[_n]"
local label`i' = string[_n]
it should work.
The latter assigns the value of string[_n] to label`i'.


----- Original Message -----
From: "R.E. De Hoyos" <>
Date: Thursday, June 2, 2005 6:54 pm
Subject: st: labels as values of a string variable

I want to attach labels to a group of variables (varlist).

The labels that I want to use for each variable are available as
observations in one of the variables (string). I tried the following:

levels string, local(labels)
tokenize `lables'

* An error occurred because the string values contained spaces.
Therefore I

egen id = group(string)    //20 groups
local i=1
while `i'<21 {
   keep if id==`i'
   local label`i' = "string[_n]"
   local i = `i'+1

When I used -di `label1'- the label names where correctly
displayed. However
when I tried to attached them to the variables using -label var- :

label var var1 "`label1'"

The label attached was "string[_n]" and not the value of it.

Many thanks,


R.E. De Hoyos
Faculty of Economics
University of Cambridge
*   For searches and help try:
*   For searches and help try:
*   For searches and help try:
Nicholas Winter 607.255.8819 t
Assistant Professor 607.255.4530 f
Department of Government e
308 White Hall w
Cornell University
Ithaca, NY 14853-4601
* For searches and help try:

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