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

From |
"Oleksandr Talavera, BC" <talaveol@bc.edu> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: Constructe a string variable |

Date |
Thu, 17 Apr 2003 02:04:31 -0400 |

There is also another solution

* N is number of digits in your format, 3 in your case

scalar N=3

g str`N' X1= substr(string(10^(N-length(string(X)))),2,N-length(string(X)))+string(X)

*----------

I tried for N=7

+--------------+

| X X1 |

|--------------|

1. | 2 0000002 |

2. | 45 0000045 |

3. | 56 0000056 |

4. | 6 0000006 |

+--------------+

Best,

Sasha

jtsheu@bu.edu wrote:

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**:**st: Reshape question***From:*Alejandro "Riaņo" <a_riano@yahoo.com>

**st: Constructe a string variable***From:*jtsheu@bu.edu

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

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