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

st: looping over strings [was: RE: Re: statalist-digest V4 #1066]


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: looping over strings [was: RE: Re: statalist-digest V4 #1066]
Date   Thu, 14 Nov 2002 11:12:45 -0000

Yan elaine Li
> 
> I would like to get help on the following problem:
> 
> i have a life history dataset on job mobility, where for each
> observation i have the following variables:
> 
> job01 start01 end01 indstr01 income01 ... job12 - income12 ...
> 
> where 'start01' means the starting year and 'end01' means the
> ending year of the first job.
> 
> I also have a variable 'yrmar' (year married), and my task is to
> find out what job a person had when s/he got married and how
> much they earn from that job and so on.
> 
> I tried the following code and it obviously doesn't work when
> you have a variable named "start01" instead of "start1":
> 
> gen jobmar=.
> gen incmar=.
> forvalues i=01(1)12 {
>   if (yrmar >= start`i' & yrmar<= end`i') {
>     replace jobmar=job`i'
>     replace incmar=income`i'
>   }
> }
> 
> I guess the central question is how to loop using strings/chars
> rather than number.
> 

Solution 1 
==========

You can fix this upstream by removing 
the leading zeroes from your variable names. 

foreach stub in job start end indstr income { 
	renvars `stub'0? \ `stub'1-`stub'9 
} 

and then go 

forvalues i=1/12 {
...
}

Here -renvars- is a program published in STB-60. 

Solution 2 
========== 

Or you can loop over strings by 
spelling them out 

foreach y in 01 02 03 04 05 06 07 08 09 10 11 12 { 
	...
} 

Simple, and quick in this example, but inelegant. 

Solution 3 
========== 

Or you could do this: 

forval j = 1/12 { 
	local i : di %02.0f `j'  
	<code in terms of `i'> 
}

The leading zero format puts a "0" in front 
of any single digit. 

Warning
======= 

However, there is a _big_ bug of a different kind 
now lurking in your code. 

if (yrmar >= start`i' & yrmar<= end`i') {
	replace jobmar=job`i'
	replace incmar=income`i'
} 

You don't mean what you write here, I guess. See
the FAQ 
I have an if command in my program that only seems to 
evaluate the first observation. What's going on?
http://www.stata.com/support/faqs/lang/ifqualifier.html

I think you need 

replace jobmar=job`i' if (yrmar >= start`i' & yrmar<= end`i') 

replace incmar=income`i'if (yrmar >= start`i' & yrmar<= end`i') 

Comment
=======

Many things are going to be easier if you -reshape- 
this data set to long. See [R] reshape and, if 
need be, the FAQ 
I am having problems with the reshape command. 
Can you give further guidance?
http://www.stata.com/support/faqs/data/reshape3.html

Nick 
[email protected] 

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