Statalist The Stata Listserver


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

Re: st: repeating strings


From   Ian Watson <[email protected]>
To   [email protected]
Subject   Re: st: repeating strings
Date   Wed, 18 Oct 2006 15:59:18 -0700

There's a typo below (the reference to name being string should have been city). In the meantime, Scott's solution is infinitely more concise and elegant.
Ian


Ian Watson wrote:

Try the following bit of code:

use test, clear
list
gen id = _n
reshape long city, i(id) j(year)
list
gsort name year -city
list
bys name year : replace city = city[1]
list
reshape wide city, i(id) j(year)
list


test is your dataset (I used the snippet of data you sent) and the list commands can be removed once you've made sure all the transformations are as you wished.

The gsort is only necessary if name is a string variable (and blanks come first). If it's a numeric code, then the missings will come last anyway, so you can replace the gsort with sort and replace the -city with city.

Regards
Ian Watson



MA V wrote:

Hello,
I believe that there is an easy answer for my problem but I can't seem to find it anywhere...
I have a table with the following format (this is a toy example where we have the cities in which each person has lived - city1 is where each person lived first):

name city1 city2 city3
John SF . .
John . Chi .
John . . LA
Mary . NY .
Mary Chi . .
...

What I would like to do is to fill in the missing values with the city strings, i.e. I would like to get at the end:

name city1 city2 city3
John SF Chi LA
John SF Chi LA
John SF Chi LA
Mary Chi NY .
Mary Chi NY .
...

Any suggestions?
Thanks!

_________________________________________________________________
Get today's hot entertainment gossip http://movies.msn.com/movies/hotgossip?icid=T002MSN03A07001

*
* 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/

*
*   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