Hi All,
I am currently writing a program and as part of the program I will need to find the maximum value of an unknown number of variables. I was hoping to feed the variable list into a local macro and the put the local macro into the max function however I have hit a few walls with this. My current issues is that I have managed to create a macro with the required text but it doesn't pick the variables up as variable names but rather as a long string. I have attached the code I have so far below (using the example of when var is 3, but in the main program var will be an unknown number):
This code causes an error as instead of passing through the numbers contained in the variables A1 A2 and A3 it passes in the string "A1, A2, A3". However if I use the code:
Then this will give me the code that I am looking for.
Thank you in advance for any advice!!
Very best wishes,
Cydney
I am currently writing a program and as part of the program I will need to find the maximum value of an unknown number of variables. I was hoping to feed the variable list into a local macro and the put the local macro into the max function however I have hit a few walls with this. My current issues is that I have managed to create a macro with the required text but it doesn't pick the variables up as variable names but rather as a long string. I have attached the code I have so far below (using the example of when var is 3, but in the main program var will be an unknown number):
Code:
gen total = _N local totlist ".," /* Create var count */ local var 3 foreach i of numlist 1/`var' { gen T`i' = 1 if rando == `i' gen T`i'count = sum(T`i' != .) bysort T`i'count: gen A`i' = _n replace A`i' = . if T`i'count == 0 if `i' != `var' { local totlist `totlist' "A`i', " di `totlist' } else if `i' == `var' { local totlist `totlist' "A`i'" di `totlist' } } local fullist "`totlist'" local fullist : subinstr local fullist ".," "", word di `fullist' gen maximum = max(`fullist')
Code:
local fullist "A1, A2, A3" gen maximum = max(`fullist')
Thank you in advance for any advice!!
Very best wishes,
Cydney
Comment