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]

From |
Nick Cox <njcoxstata@gmail.com> |

To |
"statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |

Subject |
Re: st: nested loop with string values |

Date |
Tue, 17 Dec 2013 09:55:38 +0000 |

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 njcoxstata@gmail.com On 17 December 2013 07:45, Claudia Rangel <clrangel@gmail.com> 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/

**Follow-Ups**:**Re: st: nested loop with string values***From:*Claudia Rangel <clrangel@gmail.com>

**References**:**st: nested loop with string values***From:*Claudia Rangel <clrangel@gmail.com>

- Prev by Date:
**st: nested loop with string values** - Next by Date:
**st: loop question** - Previous by thread:
**st: nested loop with string values** - Next by thread:
**Re: st: nested loop with string values** - Index(es):