Like a number of others, I'm trying to change the order of the values when converting from a string to numeric variable using encode(). I've read the manual for encode and sencode, a number of the tutorials and Q/A pairs on STATALIST (e.g., this one) about how to do this using encode(varname), label(val_lable). I seem to have a fundamental misunderstanding how the encode(varname), label(labelname) command works.
Encode without the label option converts the string in alphabetic order into integers as expected.
However, when I use encode with the label option, I don't understand the resulting order. I enter the value labels in alphabet order ala the raw encode command, and preface each value label with a number to indicate the position in which it should appear. But the resulting order is not what I expected. In the example below, I don't understand why the integer "1" wasn't used and why the integers 10 - 12 got created with no text labels attached to them even though there are only 9 values. I'd appreciate any help in helping me to understand how encode with the label option works and what I'm doing wrong.
[/CODE]
This is the order I wanted:
Encode without the label option converts the string in alphabetic order into integers as expected.
Code:
. encode(primary_utility), generate(purpose_1)
. tab purpose_1
purpose_1 | Freq. Percent Cum.
------------------------+-----------------------------------
build_new_relationships | 502 11.24 11.24
coordination | 353 7.90 19.14
emotional_support | 234 5.24 24.37
entertain | 659 14.75 39.12
existing_relationship | 493 11.03 50.16
information_support | 1,240 27.75 77.91
others_please_specify | 155 3.47 81.38
promotion | 337 7.54 88.92
transaction | 495 11.08 100.00
------------------------+-----------------------------------
Total | 4,468 100.00
. tab purpose_1, nolabel
purpose_1 | Freq. Percent Cum.
------------+-----------------------------------
1 | 502 11.24 11.24
2 | 353 7.90 19.14
3 | 234 5.24 24.37
4 | 659 14.75 39.12
5 | 493 11.03 50.16
6 | 1,240 27.75 77.91
7 | 155 3.47 81.38
8 | 337 7.54 88.92
9 | 495 11.08 100.00
------------+-----------------------------------
Total | 4,468 100.00
Code:
label define lbl_purpose 3 "new_relationships" 4 "coordination" 2 "emotional_support" ///
8 "entertain" 1 "existing_relationship" 5 "information_support" 9 "other" 6 "promotion" ///
7 "transaction"
encode primary_utility, gen(purpose_2) label(lbl_purpose)
. tab purpose_2
purpose_2 | Freq. Percent Cum.
----------------------+-----------------------------------
emotional_support | 234 5.24 5.24
coordination | 493 11.03 16.27
information_support | 659 14.75 31.02
promotion | 1,240 27.75 58.77
transaction | 337 7.54 66.32
entertain | 495 11.08 77.39
10 | 502 11.24 88.63
11 | 353 7.90 96.53
12 | 155 3.47 100.00
----------------------+-----------------------------------
Total | 4,468 100.00
. tab purpose_2, nolabel
purpose_2 | Freq. Percent Cum.
------------+-----------------------------------
2 | 234 5.24 5.24
4 | 493 11.03 16.27
5 | 659 14.75 31.02
6 | 1,240 27.75 58.77
7 | 337 7.54 66.32
8 | 495 11.08 77.39
10 | 502 11.24 88.63
11 | 353 7.90 96.53
12 | 155 3.47 100.00
------------+-----------------------------------
Total | 4,468 100.00
This is the order I wanted:
Code:
1 "existing_relationship" 2 "emotional_support" 3 "new_relationships" 4 "coordination" 5 "information_support" 6 "promotion" 7 "transaction" 8 "entertain" 9 "other"

Comment