Hi all
I am trying to use a macro to generate a 'drugs' yes(1) or no(0) variable from 3 variables coded 1yes, 0no - glue, cannabis and othersub.
Essentially if an individual has said yes to either glue, cannabis or othersub I would like drugs to be 1 - regardless if any other is missing.
If glue, cannabis or othersub no for all I would like this to be no - 0.
If glue, cannabis or othersub have a mixture of no and missing I would like to have this as a missing variable '.'
So far I have this code, but its not working - it doesn't come up with an error but all values are 0.
local druglist "glue cannabis othersub"
display "druglist"
g drugs= 0
foreach var of local druglist {
replace drugs = 1 if `var' ==1
}
foreach var of local druglist {
replace "drug = . if 'var' ==.
}
Any help would be super appreciated.
I am trying to use a macro to generate a 'drugs' yes(1) or no(0) variable from 3 variables coded 1yes, 0no - glue, cannabis and othersub.
Essentially if an individual has said yes to either glue, cannabis or othersub I would like drugs to be 1 - regardless if any other is missing.
If glue, cannabis or othersub no for all I would like this to be no - 0.
If glue, cannabis or othersub have a mixture of no and missing I would like to have this as a missing variable '.'
So far I have this code, but its not working - it doesn't come up with an error but all values are 0.
local druglist "glue cannabis othersub"
display "druglist"
g drugs= 0
foreach var of local druglist {
replace drugs = 1 if `var' ==1
}
foreach var of local druglist {
replace "drug = . if 'var' ==.
}
Any help would be super appreciated.
Comment