Destring Command (STATA)

March 24, 2014 @

Sometimes, you will find strings variable from STATA dataset. Unfortunately, you cannot do summarize, regression, ANOVA, graph, etc. However, sometimes, the dataset is saved as strings. This is a character variable, then you should to change this variable to numeric value, in order to be calculated by the program.

Simple question, how to change strings variable to numeric variable?

. d b1r1 b1r2 b1r3 b1r4 b1r5 b1r7 b1r8

storage display value
variable name type format label variable label
——————————————————————
b1r1 str2 %9s
b1r2 str2 %9s
b1r3 str3 %9s
b1r4 str3 %9s
b1r5 str1 %9s
b1r7 str7 %9s
b1r8 str2 %9s

The variables above are the strings variable. You cannot do calculation, eg:

. sum b1r1 b1r2 b1r3 b1r4 b1r5 b1r7 b1r8

Variable | Obs Mean Std. Dev. Min Max
————-+——————————————————–
b1r1 | 0
b1r2 | 0
b1r3 | 0
b1r4 | 0
b1r5 | 0
————-+——————————————————–
b1r7 | 0
b1r8 | 0

You can change to numeric variable. You can type this command:

destring b1r1 b1r2 b1r3 b1r4 b1r5 b1r7 b1r8, generate(b1r1_new b1r2_new b1r3_new b1r4_new b1r5_new b1r7_new b1r8_new) ignore(“$ ,%”)

. destring b1r1 b1r2 b1r3 b1r4 b1r5 b1r7 b1r8, generate(b1r1_new b1r2_new b1r3_new b1r4_new b1r5_new b1r7_new b1r8_new) ignore(“$ ,%”)
b1r1 has all characters numeric; b1r1_new generated as byte
b1r2 has all characters numeric; b1r2_new generated as byte
b1r3 has all characters numeric; b1r3_new generated as int
b1r4 has all characters numeric; b1r4_new generated as int
b1r5 has all characters numeric; b1r5_new generated as byte
b1r7 has all characters numeric; b1r7_new generated as long
b1r8 has all characters numeric; b1r8_new generated as byte

The result is here:

. d b1r1 b1r2 b1r3 b1r4 b1r5 b1r7 b1r8 b1r1_new b1r2_new b1r3_new b1r4_new b1r5_new b1r7_new b1r8_new

storage display value
variable name type format label variable label
—————————————————————————————————
b1r1 str2 %9s
b1r2 str2 %9s
b1r3 str3 %9s
b1r4 str3 %9s
b1r5 str1 %9s
b1r7 str7 %9s
b1r8 str2 %9s
b1r1_new byte %10.0g
b1r2_new byte %10.0g
b1r3_new int %10.0g
b1r4_new int %10.0g
b1r5_new byte %10.0g
b1r7_new long %10.0g
b1r8_new byte %10.0g

Yes, you’ve got numeric variable! Summarize, then you get:

. sum b1r1_new b1r2_new b1r3_new b1r4_new b1r5_new b1r7_new b1r8_new

Variable | Obs Mean Std. Dev. Min Max
————-+——————————————————–
b1r1_new | 277854 42.28353 24.82338 11 94
b1r2_new | 277854 22.01755 26.53184 1 79
b1r3_new | 277854 63.87709 62.68575 10 730
b1r4_new | 277854 8.370929 8.581939 1 132
b1r5_new | 277854 1.571617 .4948454 1 2
————-+——————————————————–
b1r7_new | 277854 2069906 319858.5 1500101 2599001
b1r8_new | 277854 5.461858 2.858474 1 10

 

One Comment → “Destring Command (STATA)”


  1. Eltina

    4 years ago

    Hai Kak Arip,
    Perkenalkan saya eltina alumni TIUI 2012. Saya mengirim email ke Kakak (arip@aripmuttaqien.com)untuk share pengalaman mencari beasiswa dan lanjut kuliah ke luar negeri. Mohon direspon ya Kak. Terima kasih :)


© 2018 ARIP MUTTAQIEN.