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

From |
Clare L Maxwell <maxwellcl1@earthlink.net> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
RE: st: right justifying strings, left padding strings with zeros |

Date |
Wed, 23 Nov 2005 06:00:00 -0600 |

This is perfect! Thank you so much.

I wonder if either of you can recommend a book or manual that has lots and lots of examples. It doesn't seem as if I'm getting what I should out of staring at descriptions of the string functions, for example. Ian Watson's solution is conceptually similar to how I first approached it, but I didn't get far in mapping my ideas onto the list of functions. Based on my previous problem so adroitly handled by Nick Cox and others, I decided my usual ideas about how to approach things are too colored by the data manipulation language I am coming from, and that I need to clear the mental slate. That wasn't exactly true here, but still I wasn't able to carry out what I hoped to. I would be interested in feeding my brain "monkey see, monkey do" patterns, hoping they would take hold.

Special thanks to Nick and Ian for these further examples of subinstr.

Yours truly,

Clare Maxwell

At 10:16 AM +0000 11/23/05, Nick Cox wrote:

The step-by-step approach is usually best, especially if it turns out that you want something slightly different. But note that Ian's code can be condensed to rtrim(substr(v1,1,3)) + subinstr(substr(v1,4,6)," ","0",.)) not that I would necessarily recommend that. Given a trade-off between clarity and brevity, go for clarity. Nick n.j.cox@durham.ac.uk Ian WatsonAssuming your data is in a plain text file test.txt, try the following: . insheet using test.txt (1 var, 7 obs) . list +--------+ | v1 | |--------| 1. | B 1 | 2. | B 120 | 3. | CCH 7 | 4. | CCH 23 | 5. | CCH213 | |--------| 6. | UW 23 | 7. | UW 232 | +--------+ . gen alpha=substr(v1,1,3) . list alpha +-------+ | alpha | |-------| 1. | B | 2. | B | 3. | CCH | 4. | CCH | 5. | CCH | |-------| 6. | UW | 7. | UW | +-------+ . replace alpha=rtrim(alpha) (4 real changes made) . list alpha +-------+ | alpha | |-------| 1. | B | 2. | B | 3. | CCH | 4. | CCH | 5. | CCH | |-------| 6. | UW | 7. | UW | +-------+ . gen num=substr(v1,4,6) . list num +-----+ | num | |-----| 1. | 1 | 2. | 120 | 3. | 7 | 4. | 23 | 5. | 213 | |-----| 6. | 23 | 7. | 232 | +-----+ . replace num=subinstr(num," ","0",.) (4 real changes made) . list num +-----+ | num | |-----| 1. | 001 | 2. | 120 | 3. | 007 | 4. | 023 | 5. | 213 | |-----| 6. | 023 | 7. | 232 | +-----+ . gen v2=alpha+num . list v2 +--------+ | v2 | |--------| 1. | B001 | 2. | B120 | 3. | CCH007 | 4. | CCH023 | 5. | CCH213 | |--------| 6. | UW023 | 7. | UW232 | +--------+Clare L Maxwell> I have a six-column subject identifier coming into Stata from an ASCII > file that looks like the following: > > B 1 > B 120 > CCH 7 > CCH 23 > CCH213 > UW 23 > UW 232 > > I want to read in and manipulate these 6 columns so that in the end, I > have a str6 variable that looks like this: > > B001 > B120 > CCH007 > CCH023 > CCH213 > UW023 > UW232 > > That is, the first three columns have been right justified and the last > three columns have been left-padded with zeros. I have tried various > options, but so far, not much luck. I was considering reading them in > as six str1 variables and putting things together sort of by brute > force, but I thought I'd ask first. Any suggestions on good string > manipulations for this problem?* * 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**:**RE: st: right justifying strings, left padding strings with zeros***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

- Prev by Date:
**RE: st: -rowsort- updated on SSC** - Next by Date:
**st: Test of equality of the coefficients between two samples endogeneity and IVREG2** - Previous by thread:
**RE: st: right justifying strings, left padding strings with zeros** - Next by thread:
**RE: st: right justifying strings, left padding strings with zeros** - Index(es):

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