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]

st: how to create a loop with variables starting with the same prefix


From   Joanne Lacsina <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: how to create a loop with variables starting with the same prefix
Date   Fri, 5 Apr 2013 12:04:12 -0500

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/


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