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: nested loop with string values


From   Claudia Rangel <[email protected]>
To   [email protected]
Subject   Re: st: nested loop with string values
Date   Wed, 18 Dec 2013 09:05:10 -0800

Thanks for the reply and helpful suggestions.

the revised version now works as expected.


tokenize "a b c d e f g h i j"
forval j =1/10{
gen e13mani``j'' =.
forval i =1/6{
replace e13mani``j'' = ecyma``i''ni``j''13 if ageflgecy =="``i''"
}
}

Claudia

On Tue, Dec 17, 2013 at 1:55 AM, Nick Cox <[email protected]> wrote:
> You don't explain what "not working" means precisely. I have an
> unpublished list of about 20 senses in which this phrase or one
> similar has been used on Statalist, just three of which are syntax
> error, not converging and giving results that are incorrect.
>
> Both your loops are in terms of an index i. You refer to an index j
> which is never defined. That is not illegal, but Stata will just
> substitute a blank. I guess at a typo, but as both inner loop and
> outer loop refer to j, there is likely to be a logic error there too.
>
> Your problem may call for a -reshape long-, not the creation of more
> variables, but I don't understand your data structure at all. Perhaps
> you should give us an example of your data, explaining what is to be
> an observation. For example, are individual observations people and
> values present only if the person is in a particular age group or
> income group? In that case, one of the row functions in -egen- may
> help.
> Nick
> [email protected]
>
>
> On 17 December 2013 07:45, Claudia Rangel <[email protected]> wrote:
>> Dear Stata list members
>>
>> apologies for the newbie question, but I have spent two days reading
>> and my first attempt at a nested loop is still not working.
>>
>> I have a long list of variables  (i.e. ecymacnia13 ecymacnib13
>> ecymacnic13 ecymadnia13 ecymadnib13 ecymadnic13), which basically
>> identify age groups "a b...f" and income groups "a b c...j".
>>
>> I am trying to generate one variable holding the income values
>> according to the age group for each observation on the dataset (in
>> variable ageflgecy: a (<34) b (35-54) c(45-64)...), and hopefully
>> reducing the  5 X 10 variables to just 10 income variables.
>>
>> I was able to create a similar loop but the variable names contain
>> numbers instead of strings for the age and income groups.
>>
>> This is one of my latest attempts:
>>
>>
>> tokenize "a b c d e f g h i j"
>> forval i =1/10  {
>> gen ecymani``j''13 =.
>> forval i =1/6{
>> replace ecymani``j''13 = ecyma`i'ni`j'13 if ageflgecy ==`i'
>>           }
>> }
>>
>>
>> Thanks for any suggestion.
>> Claudia
>> *
>> *   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