[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

From |
"Kompal Sinha" <[email protected]> |

To |
<[email protected]> |

Subject |
st: RE: Constructe a string variable |

Date |
Thu, 17 Apr 2003 13:29:47 +1000 |

```
If the variable X is in the string format:
U can try this way(may be a long way):
Gen str3 Y = string(.)
replace Y = string(0)+ string(0)+X if X< string(10)
replace Y = string(0)+X if X> string(9)& X<string(100)
replace Y = X if Y==.
drop X
rename Y X
Kompal
-----Original Message-----
From: [email protected]
[mailto:[email protected]]On Behalf Of [email protected]
Sent: Thursday, April 17, 2003 12:53 PM
To: [email protected]
Subject: st: Constructe a string variable
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/
*
* 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/
```

**References**:

- Prev by Date:
**st: list a variable** - Next by Date:
**st: SUR on stacked data** - Previous by thread:
**st: Constructe a string variable** - Next by thread:
**Re: st: Constructe a string variable** - Index(es):

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