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: looping through variables that are not consecutive and have common stem in name


From   "Sarah Edgington" <[email protected]>
To   <[email protected]>
Subject   RE: st: looping through variables that are not consecutive and have common stem in name
Date   Wed, 15 Jan 2014 14:35:11 -0800

David,
You shouldn't need the foreach part because each iteration of j results in
only one time variable.  So your foreach loop only contains one element.

This should get you what you want:

forv j=1/164 {
	replace count_sub300=count_sub300+ (time`j'<300)
}

If you were using the foreach loop you'd want -foreach v in time`j'-.
That's the other error that's probably leading to it "not working" (which is
too vague a description of the problem to be helpful in figuring out what's
going wrong).

-Sarah


-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of David de Jong
Sent: Wednesday, January 15, 2014 2:07 PM
To: [email protected]
Subject: Re: st: looping through variables that are not consecutive and have
common stem in name

Oh geez, I wasn't kidding that I needed coffee. Apologies for the mess. Let
me go back to the problem. The reason why I framed the question as I did was
that I'd like to be able to run the loop across a subset of the time
variables. This is why I imagine something like "forval j = 1/164" would be
useful.

gen count_sub300 = 0

qui forval j = 1/164 {

qui foreach v time`j' {

replace count_sub300 = count_sub300 + (`v' < 300)

}

}

Nick, any advice? Thanks!
~
David C. de Jong, MA
Department of Clinical and Social Sciences in Psychology University of
Rochester
214 Meliora Hall
Rochester, NY 14627


On Wed, Jan 15, 2014 at 4:58 PM, David de Jong <[email protected]>
wrote:
> Hi Nick,
>
> Thanks for all of that, and for pointing out the variable mix-up. Time 
> for my afternoon coffee, apparently.
>
> Much appreciated!
>
> Best,
>
> David
> ~
> David C. de Jong, MA
> Department of Clinical and Social Sciences in Psychology University of 
> Rochester
> 214 Meliora Hall
> Rochester, NY 14627
>
>
> On Wed, Jan 15, 2014 at 4:25 PM, Nick Cox <[email protected]> wrote:
>> Something this should be sufficient:
>>
>> gen count_sub300 = 0
>>
>> qui foreach v of var correct* {
>>      replace count_sub300 = count_sub300 + (`v' < 300) }
>>
>> Notes:
>>
>> 1. You said that you were only interested in the -time*- variables, 
>> but your code looks at the -correct*- variables.
>>
>> 2. `v' < 300 & `v' < . reduces to `v' < 300.
>>
>> Nick
>> [email protected]
>>
>> On 15 January 2014 21:12, David de Jong <[email protected]> wrote:
>>
>>> I have the variables time1 correct1 time2 correct2 etc.
>>>
>>> I'd like to loop through only the time variables, counting values 
>>> that are under 300 and not missing. I'm trying to use something like
this:
>>>
>>> qui forval j = 1/164 {
>>>
>>> qui foreach v correct`j' {
>>>
>>> replace count_sub300 = count_sub300 + (`v' < 300 & `v' < .)
>>>
>>> }
>>>
>>> }
>>>
>>> It's not working. I'm quite sure that the 2nd line is not correct, 
>>> but can't figure out what it should be. Can anyone advise?
>> *
>> *   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/

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