Hello, I find almost always an answer in this forum. This time I didn't find an answer, that's why I ask here. I searched for similar problems, but couldn't find any.
The problem is as follows. I have a local containing several variablenames with the same prefix, say var1,...,var100.
I want to calculate the sum of each variable if the observation exceeds some benchmark, because I only want to keep those variables.
Problem1: I would like to have a new variable containing the names of the local vars (var1, var2,...,var100) in the first hundred rows. Is this possible?
The following didn't work.
Problem2: I only want to keep those variables, that are "best" for subsequent calculations.
Example: when best=1 for var36 and var62 and best=0 for the other 98 variables.
Is there a command to keep only var36 and var62 and drop the other ones?
I tried:
Of course I could type something like, but that seems tedious.
Is there a easier way? Like to catch those best variables?
I have this dummy variable best that is 1 only in two rows. Maybe there is an easier way to keep the variables by using only those rows, where the dummy=1.
Many tanks in advance for your help,
Patrick
The problem is as follows. I have a local containing several variablenames with the same prefix, say var1,...,var100.
I want to calculate the sum of each variable if the observation exceeds some benchmark, because I only want to keep those variables.
Code:
unab vars: var* foreach var in `vars' { local i = `i' + 1 gen temp1=0 replace temp1=1 if `var'>benchmark egen temp2=sum(temp1) local sumtotal`i'=temp2 drop temp* } capture set obs `i' gen sumtotal = . forval j = 1/`i' { replace sumtotal = `sumtotal`j'' in `j' } gen best=0 replace best=1 if sumtotal>5 & sumtotal!=. save "${stata}\temp.dta", replace
Problem1: I would like to have a new variable containing the names of the local vars (var1, var2,...,var100) in the first hundred rows. Is this possible?
The following didn't work.
Code:
use "${stata}\temp.dta", clear gen varname=. forvalues k=1/100{ replace varname=`vars' in `k' }
Problem2: I only want to keep those variables, that are "best" for subsequent calculations.
Example: when best=1 for var36 and var62 and best=0 for the other 98 variables.
Is there a command to keep only var36 and var62 and drop the other ones?
I tried:
Code:
unab varlist : _all unab exclude : var* local best="var36 var62" local varlist : list varlist - exclude + best keep `varlist'
Code:
drop var1 var2 ... var35 var37... var61 var63 ... var100
I have this dummy variable best that is 1 only in two rows. Maybe there is an easier way to keep the variables by using only those rows, where the dummy=1.
Many tanks in advance for your help,
Patrick
Comment