# need help with "if statement"

#### nostat

##### New Member
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

##### Ninja say what!?!
I'm not that good at STATA, but have you tried

replace mbelm=1 if (bel==1)*(ausbel==1)

#### nostat

##### New Member
I'm not that good at STATA, but have you tried

replace mbelm=1 if (bel==1)*(ausbel==1)

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

##### Ninja say what!?!
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

##### RoboStataRaptor
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

##### New Member
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

##### RoboStataRaptor
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

##### New Member
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