Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

RE: st: FORVAL loop incomplete


From   "Francis, Richard N" <[email protected]>
To   "[email protected]" <[email protected]>
Subject   RE: st: FORVAL loop incomplete
Date   Wed, 20 Nov 2013 00:27:05 +0000

Nick,

My bad, I'm not communicating the problem very well.

Assume that the pgm makes 3 loops and creates one statistic for each loop.

So, each group now has one statistic.

I envision creating a new dataset which contains 2 variables: the statistic and the group ID.

How would I add the group ID to accompany the statistic?

I apologize for the lack of Stata competency.

Rick Francis


-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Nick Cox
Sent: Tuesday, November 19, 2013 4:17 PM
To: [email protected]
Subject: Re: st: FORVAL loop incomplete

As I understand it you want to supplement or replace

forval i = 1/`limit' {
        display "Iteration of `i' of `limit"

The information of interest is in the value label, so this line can follow or replace the line just quoted above.

      di "`: label (group) `i''"

The syntax from inside outwards is

`i'      group number

`: label (group) `i''   its value label

"`: label (group) `i''"   string delimiters for tidiness, at least

The syntax is explained at -help macro- and its link -help extended_fcn-.

Here is an accessible test case:

. sysuse auto, clear
(1978 Automobile Data)

. egen group = group(foreign rep78), label
(5 missing values generated)

. su group, meanonly

. forval i = 1/`r(max)' {
  2. di "`: label (group) `i''"
  3. }
Domestic 1
Domestic 2
Domestic 3
Domestic 4
Domestic 5
Foreign 3
Foreign 4
Foreign 5

Nick
[email protected]


On 19 November 2013 23:02, Francis, Richard N <[email protected]> wrote:
> Both Nick and Sergiy have provided extremely useful ideas.
>
> Have one other neophyte question ...
>
> Say the data has 3 unique IDs, and each ID has 60 unique values.
>
> The code below (between the two horizontal lines) generates summary results for each of the IDs.
>
> How would I add the contents of the ID variable to the summary results?
>
> Thank you so much!
>
> Rick
>
> ________________________________________
>
> destring gvkey, replace
>
> egen id = group(gvkey), label
>
> gen date1 = yq(year, fqtr)
>
> tsset id date1, quarterly
>
> su id, meanonly
>
>
> g Z14 = .
> g Z24 = .
> g Z34 = .
> g Z4t = .
> g Z3t = .
> g Z2t = .
> g Z1t = .
>
> local limit=r(max)
>
> forval i = 1/`limit' {
>         display "Iteration of `i' of `limit"
>
>         srootfix fcf if id == `i'
>
>         local p=`i'
>
>         replace Z14 = r(Z14) in `p'
>         replace Z24 = r(Z24) in `p'
>         replace Z34 = r(Z34) in `p'
>         replace Z4t = r(Z4t) in `p'
>         replace Z3t = r(Z3t) in `p'
>         replace Z2t = r(Z2t) in `p'
>         replace Z1t = r(Z1t) in `p'
>
> }
> _______________________________________
>
> -----Original Message-----
> From: [email protected] 
> [mailto:[email protected]] On Behalf Of Sergiy 
> Radyakin
> Sent: Saturday, November 09, 2013 11:40 AM
> To: [email protected]
> Subject: Re: st: FORVAL loop incomplete
>
> Nick, sorry I missed your message and I trusted sroot was cleared/tested. That explains a lot, but still I would isolate the problem within sroot, and keep my program clear from any crunches to handle sroot's problem. A quick fix could be something like this:
>
> http://radyakin.org/statalist/2013110901/srootfix.ado
>
> Now Rick can call srootfix instead of sroot in the loop I provided. If sroot ever gets fixed, Rick would replace that call from srootfix to sroot directly. If Rick routinely uses sroot, srootfix might become reusable in multiple projects.
> It may take like 10 more minutes to produce an automatic srootfix that would detect the version of sroot and decide whether to call it directly or use preserve, but I doubt anyone needs this level of automation.
>
> Best, Sergiy Radyakin
>
>
>
> On Sat, Nov 9, 2013 at 9:57 AM, Nick Cox <[email protected]> wrote:
>> An earlier thread established that -sroot- (SJ) does not support -if- 
>> correctly. That's the entire point of the work-around being used here.
>> Nick
>> [email protected]
>>
>>
>> On 9 November 2013 17:52, William Buchanan <[email protected]> wrote:
>>> Hi Rick,
>>>
>>> I've adjusted some of your code below based on the information that you've provided to the listserv:
>>>
>>> destring gvkey, replace
>>>
>>> encode gvkey, gen(id)
>>>
>>> gen date = yq(year, fqtr)
>>>
>>> tsset id date, quarterly
>>>
>>> g Z14 = .
>>> g Z24 = .
>>> g Z34 = .
>>> g Z4t = .
>>> g Z3t = .
>>> g Z2t = .
>>> g Z1t = .
>>>
>>> su id, meanonly
>>>
>>> forval i = 1/3 {
>>>    sroot fcf if id==`i'
>>>         replace Z14 = r(Z14) if id== `i'
>>>         replace Z24 = r(Z24) if id== `i'
>>>         replace Z34 = r(Z34) if id== `i'
>>>         replace Z4t = r(Z4t) if id==`i'
>>>         replace Z3t = r(Z3t) if id==`i'
>>>         replace Z2t = r(Z2t) if id== `i'
>>>         replace Z1t = r(Z1t) if id== `i'
>>> }
>>>
>>> You could also take Sergiy's suggestion and store everything in matrices, and if you wanted you could easily modify the user-written program around line 306 to store all of the values in a matrix that you could return in the same way as the scalars that you are referencing here.  If you wanted to create a new dataset based on the results, you could possibly modify the program to make it byable and use -statsby-, or you could check out -post- and -postfile- commands to store the results to another file.
>>>
>>> HTH,
>>> Billy
>>> On Nov 9, 2013, at 10:41 AM, "Francis, Richard N" <[email protected]> wrote:
>>>
>>>> Hi Sergiy,
>>>>
>>>> Thank you for taking time out of your day for such a novice issue.
>>>>
>>>> The entire pgm looks like this:
>>>>
>>>>
>>>> destring gvkey, replace
>>>>
>>>> egen id = group(gvkey), label
>>>>
>>>> gen date = yq(year, fqtr)
>>>>
>>>> tsset id date, quarterly
>>>>
>>>> su id, meanonly
>>>>
>>>> g Z14 = .
>>>> g Z24 = .
>>>> g Z34 = .
>>>> g Z4t = .
>>>> g Z3t = .
>>>> g Z2t = .
>>>> g Z1t = .
>>>>
>>>> forval i = 1/`r(max)' {
>>>>    preserve
>>>>    keep if id == `i'
>>>>    sroot fcf
>>>>       return list
>>>>       ereturn list
>>>>       replace Z14 = r(Z14) in `i'
>>>>       replace Z24 = r(Z24) in `i'
>>>>       replace Z34 = r(Z34) in `i'
>>>>       replace Z4t = r(Z4t) in `i'
>>>>       replace Z3t = r(Z3t) in `i'
>>>>       replace Z2t = r(Z2t) in `i'
>>>>       replace Z1t = r(Z1t) in `i'
>>>>       append using new
>>>>    restore
>>>> }
>>>>
>>>> R(max) immediately prior to the FORVAL loop is three (3), but I'm not sure this is what you are looking for in your first comment.
>>>>
>>>> Does this help at all?
>>>>
>>>> Again, thank you!
>>>>
>>>> Rick Francis
>>>>
>>>> -----Original Message-----
>>>> From: [email protected]
>>>> [mailto:[email protected]] On Behalf Of Sergiy 
>>>> Radyakin
>>>> Sent: Saturday, November 09, 2013 9:28 AM
>>>> To: [email protected]
>>>> Subject: Re: st: FORVAL loop incomplete
>>>>
>>>> Rick,
>>>> 1) N may be 180, but what is r(max)? In Stata r(max) is the result of the previous command [of r-class]. You don't show us this command.
>>>> Please do.
>>>> 2) The combination of "append using new" and "restore" does not make sense, Whatever you append you immediately lose.
>>>> 3) If append is meant to add one observation where you are saving 
>>>> the results (dataset new is a blank one observation of the same 
>>>> layout as the current dataset), consider using: expand in _N
>>>> 4) If you want to accumulate the results of your tests, consider using a matrix to hold the results, and avoid modifying the data.
>>>> Best, Sergiy
>>>>
>>>> On Sat, Nov 9, 2013 at 8:14 AM, Francis, Richard N <[email protected]> wrote:
>>>>> I'm sorry, N = 180 obs, currently cycles through the first 60 (3 groups of 60 =180 total obs).
>>>>>
>>>>> Thank you!!
>>>>>
>>>>> Rick Francis
>>>>>
>>>>> -----Original Message-----
>>>>> From: [email protected]
>>>>> [mailto:[email protected]] On Behalf Of Nick 
>>>>> Cox
>>>>> Sent: Saturday, November 09, 2013 9:08 AM
>>>>> To: [email protected]
>>>>> Subject: Re: st: FORVAL loop incomplete
>>>>>
>>>>> Tell us what r(max) is.
>>>>> Nick
>>>>> [email protected]
>>>>>
>>>>>
>>>>> On 9 November 2013 15:53, Francis, Richard N <[email protected]> wrote:
>>>>>> Hello Statalist,
>>>>>>
>>>>>> Have a simple FORVAL loop which should result in multiple cycles.
>>>>>>
>>>>>> However, she only cycles once. I'm sure the answer is obvious to experienced Stata users (which I am not).
>>>>>>
>>>>>> The loop is as follows:
>>>>>>
>>>>>>
>>>>>> forval i = 1/`r(max)' {
>>>>>>    preserve
>>>>>>    keep if id == `i'
>>>>>>    sroot fcf
>>>>>>               return list
>>>>>>               ereturn list
>>>>>>               replace Z14 = r(Z14) in `i'
>>>>>>               replace Z24 = r(Z24) in `i'
>>>>>>               replace Z34 = r(Z34) in `i'
>>>>>>               replace Z4t = r(Z4t) in `i'
>>>>>>               replace Z3t = r(Z3t) in `i'
>>>>>>               replace Z2t = r(Z2t) in `i'
>>>>>>               replace Z1t = r(Z1t) in `i'
>>>>>>               append using new
>>>>>>    restore
>>>>>> }
>>>>>>
>>>>>> I am grateful for any thoughts anyone may have.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/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/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index