| 
    
 |   | 
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
Re: Re: Re: st: Creating a indicator of the number of job with highest
Here is one solution that is not messed up by
any missings:
reshape long w_job, i(ID) j(jobnum)
bysort id (w_job): ///
	gen Number_job = cond(w_job < ., jobnum, jobnum[_n-1])
by id : replace Number_job = Number_job[_N]
reshape wide w_job, i(ID) j(jobnum)
This still leaves yet another problem: if two or more
jobs tie for maximum wage, then only the one with the
highest ID is recorded.
I forgot to mention two packages on SSC in this terrain,
-rowsort- and -rowranks-. The latter is a bit smarter about
ties.
That said, people interested in this problem because it resembles
theirs probably should consider a -reshape- that is not reversed.
The data here look like panel or longitudinal data and are better
off long.
Nick
[email protected]
Nick Cox
The -reshape- idea is good. It can be slimmed
slightly:
reshape long w_job, i(ID) j(jobnum)
bysort id (w_job): gen Number_job = jobnum[_N]
reshape wide w_job, i(ID) j(jobnum)
but this solution, like Kelvin's, will return
any missing for each -id- as the maximum.
There are various ways around that; just be warned.
The calculation can also be done without a
-reshape-. We can also trap any missings that way.
gen max = w_job1
gen Number_job = cond(max == ., ., 1)
qui forval i = 2/4 {
	replace max = max(max, w_job`i')
	replace Number_job = `i' if max == w_job`i'
}
Kelvin Foo
reshape long w_job, i(ID) j(jobnum)
gsort id -w_job
by id: gen Number_job=jobnum[1]
reshape wide w_job, i(ID) j(jobnum)
Hui Wang
 > I have a dataset. Each individual can at most have 4 jobs
(job1-job4). For
 > each individual I know the hourly wage for each job (w_job1-w_job4). I
 > wonder how I can creat a variable indicating that which job has the
highest
 > wage. Refer to the following example. The variable I want to creat 
is the
 > 'Number_job'
 >
 > ID w_job1 w_job2 w_job3 w_job4 Number_job
 > 1  10        100      0          0         2
 > 2  120      30        10        0         1
 > 3  20        90        80        70       2
*
*   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/