Dear Eric, dear Martin, dear all
Thanks for your comments and help. Now it is running perfectly. For everybody having such a problem to create from a series of variables a date variable using a flexible end of a forvalues list - please find below the complete code.
Again the problem was that one haves a varlist pat_se1, patse_se2, ..., where months are in the odd pat_se variables and years are in the even pat_se variables. Now you need to program a loop and want to use the forvalues i=1/... command. However you want to have the end of list not to be fixed, but more flexible. To be more precise, the end of the list should be half the number of the variables involved in that loop.
quietly: egen no_vars1=anycount(pat_se*), values(1/12 1980/2015)
quietly: egen no_vars2=max(no_vars1)
levelsof no_vars2, local(r)
drop no_vars*
forvalues i=1/`=`r'/2'{
local j=`i'*2
local k=`j'-1
local t=round(`i'/2)
gen PATENTS_SH`i' = ym(pat_se`j', pat_se`k')
format PATENTS_SH`i' %tmn_CY
if mod(`i',2)==0 {
lab var PATENTS_SH`i' " Patenting, end `t'. spell, real time "
}
else lab var PATENTS_SH`i' " Patenting, start `t'. spell, real time "
}
In the end you receive a specific number of variables PATENTS_SH* which contains the dates of the beginning and the end of the respective business starting activity labelled "Patenting".
Michael
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Eric A. Booth
Sent: Friday, 14 August 2009 1:08 AM
To: [email protected]
Subject: Re: AW: st: AW: Flexible end of numlist
Obviously it doesn't. I didn't read that part of Michael's post
carefully, and so my suggestion was simply aimed at making the end of
his numlist flexible ... apologies to Michael for any confusion it
might have caused.
Best,
Eric
__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
[email protected]
Office: +979.845.6754
Fax: +979.845.0249
On Aug 13, 2009, at 9:47 AM, Martin Weiss wrote:
>
> <>
>
> Well, I wanted to leave open the possibility that he might tweak my
> code to
> accomodate his stated wish that "the end of the numlist should be
> half the
> number of the variables involved in that loop." How does your proposal
> address this issue?
>
>
>
>
> HTH
> Martin
>
>
> -----Ursprüngliche Nachricht-----
> Von: [email protected]
> [mailto:[email protected]] Im Auftrag von Eric A.
> Booth
> Gesendet: Donnerstag, 13. August 2009 16:45
> An: [email protected]
> Betreff: Re: st: AW: Flexible end of numlist
>
> MIchael may not want to pass the -forvalues- loop a value from another
> -local-, in which case using " `=_N' " could do the trick:
>
> *****
>
> forvalues i=2/`=_N' {
> di in red `i'
> }
> *****
>
>
> Best,
>
> Eric
>
> __
> Eric A. Booth
> Public Policy Research Institute
> Texas A&M University
> [email protected]
> Office: +979.845.6754
> Fax: +979.845.0249
>
>
> On Aug 13, 2009, at 2:31 AM, Martin Weiss wrote:
>
>>
>> <>
>>
>> The help advises against this use of the -foreach- loop structure.
>> See -h
>> foreach-:" If you wish to loop over many equally spaced values, do
>> not
>> code...", instead use -forvalues-. Apart from that, you can have
>> Stata
>> evaluate an -expression- as the endpoint of a -numlist-, in this
>> particular
>> case half the -local- "r":
>>
>>
>> *************
>> local r 10
>>
>> forv i=1/`=`r'/2'{
>> di in red `i'
>> }
>> *************
>>
>>
>>
>> HTH
>> Martin
>>
>> -----Ursprüngliche Nachricht-----
>> Von: [email protected]
>> [mailto:[email protected]] Im Auftrag von Michael
>> Stuetzer
>> Gesendet: Donnerstag, 13. August 2009 02:40
>> An: [email protected]
>> Betreff: st: Flexible end of numlist
>>
>> Dear stata listers,
>>
>> I need to program a loop and want to use the "foreach i of numlist
>> 1/10"
>> begin. However I want to have the end of numlist not to be fixed,
>> but more
>> flexible. To be more precise, the end of the numlist should be half
>> the
>> number of the variables involved in that loop. Does anyone have an
>> idea how
>> to do that?
>>
>> The variables which are involved in that loop are for instance
>> pat_se1
>> pat_se2 pat_se3 pat_se4 and contain the month (pat_se[odd]) and the
>> years
>> (pat_se[even]) of a specific business starting activity. I have
>> here a
>> first draft of that loop
>>
>>
>> foreach i of numlist 1/2 {
>> local j=`i'*2
>> local k=`j'-1
>> local t=round(`i'/2)
>> gen PATENTS_SH`i' = ym(pat_se`j', pat_se`k')
>> format PATENTS_SH`i' %tmn_CY
>> if mod(`i',2)==0 {
>> lab var PATENTS_SH`i' "Patenting, end `t'. spell, real time"
>> }
>> else lab var PATENTS_SH`i' "Patenting, start `t'. spell, real time"
>> }
>>
>> Thanks for your help
>> Michael
>>
>>
>> *
>> * 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/