Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: foreach question


From   "Martin Weiss" <martin.weiss1@gmx.de>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: foreach question
Date   Sun, 22 Aug 2010 23:24:05 +0200

<>


Maybe I am missing the point, but where is the relation to your original
question some posts ago? Anyway, -sort-ing within "id" is best accomplished
via -bysort-, as in NJC`s
http://www.stata-journal.com/sjpdf.html?articlenum=pr0004

I think you should separate "year_jobnum" and hold it as two numeric vars:

***
clear*

inp str6 year_jobnum
1997_1     
1997_2
1997_3
1998_1     
1998_2     
1998_3
1997_1     
1997_2
1997_3
1998_1     
1998_2
1998_3
1997_1
1997_2
1997_3
1998_1     
1998_2     
end

split year_jobnum, parse(_) destring
ren  year_jobnum1 year
ren year_jobnum2 jobnum
list, noo
***


HTH
Martin


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of David Torres
Sent: Sonntag, 22. August 2010 23:10
To: statalist@hsphsun2.harvard.edu
Subject: RE: st: foreach question

Martin,

I originally did have the date in long format (see my yet unanswered  
question of yesterday).

I'm using the NLSY97 employment data.  I've reshaped wide to long  
using the string option because I have several jobs per year for  
several years of data.  The year_jobnum is my string variable.

id        year_jobnum      stjob           endjob           employerid
1          1997_1          01Jan1996      06July1997          9701
1          1997_2
1          1997_3
1          1998_1          01Jan1996      25June1998          9701
1          1998_2          10June1997      19Aug1997          9801
1          1998_3
2          1997_1          15Sept1996      30June1997         9713
2          1997_2
2          1997_3
2          1998_1          22Jan1997      15July1998          9820
2          1998_2
2          1998_3
3          1997_1
3          1997_2
3          1997_3
3          1998_1          11Oct1997      30July1998          9816
3          1998_2          30May1997      25Aug1997           9846
3          1998_3

I hope this is more challenging for you, sir.  And, again, I  
appreciate your assistance.  While currently the data are odered in  
each year according to end date of job (most recent or current  
employer is listed in slot 1), I want to reorder according to begin  
job date.  This seems the easiest way to backfill data for missing  
years since jobs held since date of last interview are asked in all  
rounds, meaning that income information can be gathered for missed  
years as long as a job was worked.

Thanks,
D

Quoting Martin Weiss <martin.weiss1@gmx.de>:

>
> <>
>
> This new info makes my -reshape- solution more attractive. I seriously
doubt
> that you want to work in wide format with this kind of data. Take a look
at
> the long data, and see whether it facilitates your analysis:
>
>
> ***********
> clear*
>
> inp str10(Startjob1 Startjob2 Endjob1 Endjob2)
> 01Jan1997 05June1997 24Dec1997 02Sept1997
> end
>
> gen byte id=1
> reshape long Startjob Endjob, i(id) j(number)
> drop id
> list, noo
> ***********
>
>
> HTH
> Martin
>
>
> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of David Torres
> Sent: Sonntag, 22. August 2010 22:39
> To: statalist@hsphsun2.harvard.edu
> Subject: Re: st: foreach question
>
> To complicate things a bit more, what if date1 and date2 and date3 and
> date4 represent, respectively, begin and end dates for employment.
>
> Startjob1         Startjob2          Endjob1             Endjob2
> 01Jan1997         05June1997         24Dec1997          02Sept1997
>
> Is there a way to sort both together so the correct start and end
> dates remain together?  The data I am using are currently sorted on
> endjob dates, such that the most recent employment (i.e., at time of
> interview) is listed in job#1 slot.  As you can see, however,
> sometimes a start date may precede another start date, while the end
> dates are actually reversed.
>
> Thanks in advance.
>
>
> Quoting Eric Booth <ebooth@ppri.tamu.edu>:
>
>> <>
>>
>> I think this is what you are asking:
>>
>> ******!
>> clear
>> inp  str12(Date1          Date2         Date3         Date4)
>> 23July1997    01Jan1997     12Sept1997     03Feb1997
>> 05July1997	04July1997	03July1997	02July1997
>> end
>>
>> ds
>> foreach v in `r(varlist)' {
>> 	g `v'2 = date(`v', "DMY")
>> 	format `v'2 %td
>> 	drop `v'
>> 	rename `v'2 `v'
>> 		}
>> 	g id = _n
>> reshape long Date, i(id) j(n)
>> sort Date
>> g new = _n
>> drop n id
>> g id = 1
>> reshape wide Date, i(id) j(new)
>> drop id
>> li
>> ******!
>>
>> ~ Eric
>> __
>> Eric A. Booth
>> Public Policy Research Institute
>> Texas A&M University
>> ebooth@ppri.tamu.edu
>> Office: +979.845.6754
>>
>> On Aug 22, 2010, at 2:54 PM, David Torres wrote:
>>
>>> Is there a way to sort or reorder dates using the foreach
>>> command--perhaps using the egen function in the loop or something?
>>>
>>> How do I get:
>>>
>>> Date1          Date2         Date3         Date4
>>>
>>> 23July1997    01Jan1997     12Sept1997     03Feb1997
>>>
>>> to be ordered thusly:
>>>
>>> Date1          Date2         Date3         Date4
>>> 01Jan1997     03Feb1997     23July1997     12Sept1997
>>>
>>> Thanks,
>>>
>>> --
>>> David Diego Torres
>>>
>>> *
>>> *   For searches and help try:
>>> *   http://www.stata.com/help.cgi?search
>>> *   http://www.stata.com/support/statalist/faq
>>> *   http://www.ats.ucla.edu/stat/stata/
>>
>>
>>
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/statalist/faq
>> *   http://www.ats.ucla.edu/stat/stata/
>>
>>
>>
>
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>
>
>


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


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