Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down at the end of May, and its replacement, **statalist.org** is already up and running.

[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: how to create a loop with variables starting with the same prefix |

Date |
Fri, 5 Apr 2013 18:44:14 +0100 |

Comments on two levels: A. Why did you -reshape-? From this example alone, you made things more difficult. B. There are at least three reasons your code didn't work. 1. -local i = 1/26- just evaluates the fraction 1/26 (a bit less than 0.04), which is useless for what you want to do. That's what threw you out, I guess as ... word .0384615384615385 of ... makes no sense to Stata. 2. Apart from that, the second time that Stata hit gen name2008 = 1 it would squawk, because you can't -generate- the same variable twice. 3. I think you have a typo in referring to -var3- + stub when your variables are named -var3200- + stub. Here's a sketch, but this has not been tested. tokenize "`c(alpha)'" forval x = 8/13 { gen name200`x' = 0 forval i = 1/26 { replace name200`x'=1 if (inrange(var200`x'``i'', 71, 160) & (var3200`x'``i''=="Yes")) } } The -tokenize- and -inrange()- are extra small tricks. However, I note that the constants here 71, 160 are not the same as 10, 20 earlier in your post. Nick njcoxstata@gmail.com On 5 April 2013 18:04, Joanne Lacsina <jlacsina@collegeaccessfoundation.org> wrote: > I reshaped a long dataset into a wide dataset and I am now trying to create a code that will generate a variable if it meets specified conditions. The conditional variables have the same prefixes with sequential letters. > > An example would be: > > > gen name2008=1 if (var2008<=10 & var2008<=20) & (var32008="Yes") > replace name2008=1 if (var2008a<=10 & var2008a<=20) & (var32008a="Yes") > replace name2008=1 if (var2008b<=10 & var2008b <=20) & (var32008b ="Yes") > replace name2008=1 if (var2008c<=10 & var2008c<=20) & (var32008c="Yes") > replace name2008=1 if (var2008d<=10 & var2008d<=20) & (var32008d="Yes") > > gen name2009=1 if (var2009<=10 & var2009<=20) & (var32009="Yes") > replace name2009=1 if (var2009a<=10 & var2009a<=20) & (var32009a="Yes") > replace name2009=1 if (var2009b<=10 & var2009b<=20) & (var32009b="Yes") > replace name2009=1 if (var2009c<=10 & var2009c<=20) & (var32009c="Yes") > > gen name2010=1 if (var2010<=10 & var2010<=20) & (var32010="Yes") > replace name2010=1 if (var2010a<=10 & var2010a<=20) & (var32010a="Yes") > > . > . > . > . > > gen name2012=1 if (var2012<=10 & var2012<=20) & (var32012a="Yes") > > > > I'm not sure if or how to create a loop to accomplish this. I tried this: > > forvalues x=8/13 { > local i = 1/26 > local letter : word `i' of `c(alpha)' > gen name200`x'=1 if ((var200`x'`letter'>=71 & var200`x'`letter'<=160) & (var3`x'`letter'=="Yes")) > } > > But this is wrong. I keep getting the message, syntax error. Does anyone have any suggestions? > > Thanks!! > Joanne > > > > * > * 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/

**References**:**st: how to create a loop with variables starting with the same prefix***From:*Joanne Lacsina <jlacsina@collegeaccessfoundation.org>

- Prev by Date:
**Re: st: relation between 2 coefficients in a regression analysis** - Next by Date:
**Re: st: Panel Data Within Outside Region Effects** - Previous by thread:
**st: how to create a loop with variables starting with the same prefix** - Index(es):