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

st: Constructe a string variable


From   [email protected]
To   [email protected]
Subject   st: Constructe a string variable
Date   Wed, 16 Apr 2003 22:52:37 -0400

Dear Listers:

Suppose I have a string variable with 3 position on each observations.
Each of these observations contains some values range from 1-999.
i.e.:
       X
obs1   1
obs2   12
obs3   123
(though all entries are numerical, but variable X is of string format)

My purpose is to reconstruct this variable to be the following formate:
       X
obs1   001
obs2   023
obs3   345

i.e. I want to replace the left postition for each observation with "0" if 
nothing here.

I have tried the following method:

gen str1 X1=substr(X, 1, 1)
gen str2 X2=substr(X, 2, 1)
gen str3 X3=substr(X, 3, 1)

my idea is that if I can divide original variable(X) into 3 pieces, and then I 
can replace the missing( or " ") into 0, and then re-combine the variable.

However, in the above data example.
the X1 (=substr(X,1,1) is always equal to: 

obs1 1
obs2 2
obs2 3

(the corerct one should be:
obs1  
obs2  
obs2 3

That means, the function of -string- or -substr- in Stata will always return 
the first values of that observations no matter its' real position.
I am just wondering whether anyone knows an easier function or command to deal 
with that?


*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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