Hi there, I am just going over a code given to me by my colleagues in order to learn.
Currently new to macros and loops.
Could you kindly explain to me this code
gen mst=0
foreach num of numlist 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 {
replace mst=1 if dg_`num'>="C77" & dg_`num'<="C79"
}
Line 1: Generates a new variable called mst = 0
Line 2: local Macro created called 'num'
Line 2: numlist - refers to the numbers consisting of +/- the combination of 1-20 :
1. Question --> Do the variables in numlist have to be numerical variables ?
2. What happens if they are stored as string eg Z14, Z15? Will it still work? Does numlist take into conisderation if there is a letter stored before the numbers eg Z14
3. What if the values consist of the value 300 will the macro (in bold) still work?
Line 3: replacing mst = 1 if the macro 'num' consists of values called C77 to C79
Apologies, just trying to practice with macros and loops. The help numlist on Stata isn't very helpful .
Currently new to macros and loops.
Could you kindly explain to me this code
gen mst=0
foreach num of numlist 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 {
replace mst=1 if dg_`num'>="C77" & dg_`num'<="C79"
}
Line 1: Generates a new variable called mst = 0
Line 2: local Macro created called 'num'
Line 2: numlist - refers to the numbers consisting of +/- the combination of 1-20 :
1. Question --> Do the variables in numlist have to be numerical variables ?
2. What happens if they are stored as string eg Z14, Z15? Will it still work? Does numlist take into conisderation if there is a letter stored before the numbers eg Z14
3. What if the values consist of the value 300 will the macro (in bold) still work?
Line 3: replacing mst = 1 if the macro 'num' consists of values called C77 to C79
Apologies, just trying to practice with macros and loops. The help numlist on Stata isn't very helpful .
Comment