1

I am trying to create a new string variable that combines the string of a real number (an ID) with a name. The name is a numeric variable with a value label.

Example data can be found below:

* Input Data
clear
input long num id
1 689347
2 972623
end
label values num num
label def num 1 "Label A" 2 "Label B"

 +------------------+
 |     num       id |
 |------------------|
 | Label A   689347 |
 | Label B   972623 |
 +------------------+

What I would like to do is create a string of the type 689347 - Label A. This is very easy to do by simply using decode on num, then writing a new string as follows:

tempvar numstr
decode num, gen(`numstr')
gen label = string(id) + " - " + `numstr'

 +-------------------------------------+
 |     num       id              label |
 |-------------------------------------|
 | Label A   689347   689347 - Label A |
 | Label B   972623   972623 - Label B |
 +-------------------------------------+

This is already pretty easy, but is there a way to do this in one line, without the decode command?

For example something like:

gen label = string(if) + " " + string(num)

The problem with this is, of course, that this will just give a string of the real number value (1 and 2) that num takes on.

In this post you can see how to reference the value label in an if command.

My question is:

Is there a way to tell Stata to create a string and pull the value label instead of the value?

Aaron Wolf
  • 337
  • 3
  • 13

2 Answers2

1

If you do not want to use decode, then this does the trick:

generate label = ""

forvalues i = 1 / 2 {
    replace label = string(id) + " - " + "`: label num `i''" in `i'
}
  • Note the implication, You need to loop over observations as the value label concerned will differ from observation to observation (unless exceptionally all values have the same value label). – Nick Cox Apr 19 '18 at 17:16
  • This is also more intensive than the solution I proposed. The point isn't that I am allergic to `decode`. The point is simply whether there is a way to do this on one line instead of three. – Aaron Wolf Apr 22 '18 at 10:19
  • 1
    In your post you ask: "...is there a way to do this in one line, without the decode command?...". There isn't one that can do it in one line but there is one (above) that can do it without `decode`. –  Apr 22 '18 at 11:14
  • By the way, I did not see any concerns regarding intensity voiced in your question. –  Apr 22 '18 at 11:21
1

The best I can do is two lines.

decode num, generate(label)
replace label = string(id) + " - " + label
Richard Herron
  • 9,760
  • 12
  • 69
  • 116
  • This is exactly the same as the solution I already have in the question, but without the tempvar (meaning I would need to add a third line to drop the _label_ variable anyway. – Aaron Wolf Apr 22 '18 at 10:16
  • @AaronWolf this is not exactly the same. This is two lines instead of three. Why would you drop `label`? – Richard Herron Apr 22 '18 at 13:38
  • Ah, sorry, I see what you did there. Yeah, that's probably the best we can do. – Aaron Wolf Apr 23 '18 at 06:25