I have had a problem when trying to find the maximum and minimum length of every variable in the data set without creating new variables. I have a very large data set containing both string and numeric variables. I know the egen function max(strlen(x)) alt. max(strlen(string(x))) is possible to use when only dealing with strings or numeric variables. However, I would like to avoid both creating new variables (due to time consumption) as well as converting numeric variables to strings.
Right now I have this piece of code working, but takes a long time to run:
foreach x of local xvars {
cap egen maxl_temp = max(strlen(string(`x')))}
cap egen minl_temp = min(strlen(string(`x')))
cap egen maxl_temp = max(strlen(`x'))
cap egen minl_temp = min(strlen(`x'))
local maxl = maxl_temp[1]
local minl = minl_temp[1]
cap drop maxl_temp minl_temp
Is there anyone that has encountered this dilemma before and have found a solution that is better, more efficient?
Thank you!
0 Response to Find min/max*length of variable
Post a Comment