View Full Version : need help with "if statement"
nostat
10-21-2010, 10:17 AM
Let's say there are three variables: "ausbel", "autbel", and "mbelm"
I need to replace the values of mbelm with 1, if both ausble and autbel contain 1:
replace mbelm=1 if .*bel==1
I've tried all combinations for the boolean and nothing seems to work...
Could anyone kindly suggest an alternative?
EDIT: sorry I forgot to add [STATA] to the thread title... I would truly appreciate it if one of the mods could do it
I'm not that good at STATA, but have you tried
replace mbelm=1 if (bel==1)*(ausbel==1)
nostat
10-21-2010, 11:04 AM
I'm not that good at STATA, but have you tried
replace mbelm=1 if (bel==1)*(ausbel==1)
Thanks for the reply.
I have hundreds of variables ending with "bel" and it'll be cumbersome to actually sit down and type every single one every time.
That's why I was hoping I could just use *bel or .*bel instead to refer to all variables whose names end with "bel"
Thank you again
I don't know if this will help here and for STATA, so you'll have to look it up more on your own (sorry). But for SAS, when there are a lot of variables, I can use hyphens. The condition is that the variables end with numbers (thus you would have to rename them). For example, if I have 100 variables (x1,x2,...,x100), I could just type in x1-x100.
HTH
bukharin
10-24-2010, 07:06 AM
This will make mbelm 1 if *all* variables ending in bel are 1, and 0 otherwise.
gen mbelm = 1
foreach var of varlist *bel {
replace mbelm=0 if `var'!=1
}
nostat
10-24-2010, 04:19 PM
This will make mbelm 1 if *all* variables ending in bel are 1, and 0 otherwise.
gen mbelm = 1
foreach var of varlist *bel {
replace mbelm=0 if `var'!=1
}
It doesn't work but it certainly is helpful! Thank you so much...
Would you please explain what 'var'!=1 does? Maybe that's why it's not working...
bukharin
10-25-2010, 01:15 AM
It's not 'var', it's `var'
The first quote is a left-quote (usually next to 1 on your keyboard)
As for what it does,
foreach var of varlist *bel {
This creates a list of variables (a "varlist"), consisting of each variable whose name ends in bel. Then for each of those variables, it runs the command inside the {}. The command inside the {} references the variable for each iteration using `var'
eg `var' might be ausbel for the first iteration, autbel for the second iteration etc
Etienne
10-25-2010, 03:57 PM
Hey there,
I'm not sure that you can use * or ? with varlist. So first you might need to create a local macro with the name of the variables. Let's say you want the name of the list to be vlist. Then type:
unab vlist : *bel
gen mbelm = 1
foreach var of local vlist {
replace mbelm = 0 if `var' != 1
}
Hope this helps!
Etienne
bukharin
10-26-2010, 07:19 AM
I'm not sure that you can use * or ? with varlist
You definitely can in version 11:
http://www.stata.com/help.cgi?varlist
Not sure about earlier versions...
Powered by vBulletin™ Version 4.1.3 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.