Hello all,
I am trying to drop a variable conditionally on it taking certain values in some of its observations. In the spirit of the prohibited:
drop varname if substr(varname, a, b)=="some string value" (for a string variable).
Searching the forum I found a way to do this via the following piece of code:
foreach c of varlist _all {
if substr(`c',1,3)=="Nov" | substr(`c',1,3)=="Oct" | substr(`c',1,3)=="Dec" | substr(`c',1,3)=="Aug" | substr(`c',1,3)=="Jun" | substr(`c',1,3)=="Jul" | substr(`c',1,3)=="Feb" | substr(`c',1,3)=="Mar" | substr(`c',1,3)=="Jan" | substr(`c',1,3)=="Sep" | substr(`c',1,3)=="Apr" | substr(`c',1,3)=="May" {
drop `c'
}
}
note1: i used all months to extinguish all the possible string values the variable in question can take. (if I had put only 1 or 2 it didn't work, no error, just didn't drop the variable in question)
note2: also in the above variable there were spaces (as in one below), but these didn't create a problem when not specified.
note 3: i am doing this for all possible variables as I need to loop this procedure over different files and the name of the variable changes but the string values it can take don't.
The above code worked just fine. However when i tried, in the same spirit, to do the same for another variable (below), which takes 3 string values: " * " , "-" , " " . ( Its type is str78)
Code:
foreach c of varlist _all {
if substr(`c',1,1)=="*" | substr(`c',1,1)=="-" {
drop `c'
}
}
I am returned the error 109: type mismatch
In an expression, you attempted to combine a string and numeric
subexpression in a logically impossible way. For instance, you
attempted to subtract a string from a number or you attempted
to take the substring of a number.
Thus, I believe the reason stata would execute the first piece of code but not the second, may be that stata recognizes the charachter " * " as numeric rather than a string.
Due to this, I tried without using substr:
foreach c of varlist _all {
if `c'=="*" | `c'=="-" {
drop `c'
}
}
And this runs without errors, but does not yield the desired result, ie does not drop the variable in question. (I believe because this works over observations rather than variables)
Any assistance is greatly appreciated.
I am trying to drop a variable conditionally on it taking certain values in some of its observations. In the spirit of the prohibited:
drop varname if substr(varname, a, b)=="some string value" (for a string variable).
Searching the forum I found a way to do this via the following piece of code:
foreach c of varlist _all {
if substr(`c',1,3)=="Nov" | substr(`c',1,3)=="Oct" | substr(`c',1,3)=="Dec" | substr(`c',1,3)=="Aug" | substr(`c',1,3)=="Jun" | substr(`c',1,3)=="Jul" | substr(`c',1,3)=="Feb" | substr(`c',1,3)=="Mar" | substr(`c',1,3)=="Jan" | substr(`c',1,3)=="Sep" | substr(`c',1,3)=="Apr" | substr(`c',1,3)=="May" {
drop `c'
}
}
note1: i used all months to extinguish all the possible string values the variable in question can take. (if I had put only 1 or 2 it didn't work, no error, just didn't drop the variable in question)
note2: also in the above variable there were spaces (as in one below), but these didn't create a problem when not specified.
note 3: i am doing this for all possible variables as I need to loop this procedure over different files and the name of the variable changes but the string values it can take don't.
The above code worked just fine. However when i tried, in the same spirit, to do the same for another variable (below), which takes 3 string values: " * " , "-" , " " . ( Its type is str78)
Code:
foreach c of varlist _all {
if substr(`c',1,1)=="*" | substr(`c',1,1)=="-" {
drop `c'
}
}
I am returned the error 109: type mismatch
In an expression, you attempted to combine a string and numeric
subexpression in a logically impossible way. For instance, you
attempted to subtract a string from a number or you attempted
to take the substring of a number.
Thus, I believe the reason stata would execute the first piece of code but not the second, may be that stata recognizes the charachter " * " as numeric rather than a string.
Due to this, I tried without using substr:
foreach c of varlist _all {
if `c'=="*" | `c'=="-" {
drop `c'
}
}
And this runs without errors, but does not yield the desired result, ie does not drop the variable in question. (I believe because this works over observations rather than variables)
Any assistance is greatly appreciated.
Comment