Hello everyone. For a project, I have to clean my dataset by manually selecting some values of a variable called varname. To do this, I'm using the -levelsof- command associated with the condition that the function strpos(varname, "whatever I want to modify here") is positive. However for a reason I can't understand, when I enter the value of strpos to be detected, even when it is exactly pasted, the code doesn't detect the local in which it is saved. In other cases though, as you will see, the code runs just fine. Please consider these two observations of my dataset :
I do :
What's strange is that now that I replicated this example based solely on this thread on Stata to check, the code works fine, and I didn't even edit anything! But I'm 100% sure it doesn't work on my do-file, since I used the command -set trace on- to know where the code stops. Here are the results:
For the one that works:
I wonder what the problem could be... Could anyone help me ?
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str1227 varname "Avez-vous un agrément pour votre activité secondaire ?" "Votre structure a-t-elle des partenariats professionnels avec des structures en dehors de votre site d’accueil ? " end
I do :
Code:
// This one doesn't work levelsof varname if strpos(varname, "Votre structure a-t-elle des partenariats professionnels avec des structures en dehors de votre site d'accueil ? ") > 0, local(C5) local N_C5 = r(N) // This one works : levelsof varname if strpos(varname, "Avez-vous un agrément pour votre activité secondaire ?") > 0, local(A23) local N_A23 = r(N)
Code:
- levelsof varname if strpos(varname, "Votre structure a-t-elle des partenariats professionnels avec des structures en dehors de
> votre site d'accueil ? ") > 0, local(C5)
------------------------------------------------------------------------------------------------------------ begin levelsof ---
- version 15.0
- syntax varname [if] [in] [, Separate(str) MISSing Local(name local) Clean MATROW(name) MATCELL(name) HEXadecimal ]
- if ("`missing'" == "") {
= if ("" == "") {
- marksample touse, strok
- }
- else {
marksample touse, strok novarlist
}
- if (`"`separate'"' == "") {
= if (`""' == "") {
- local separate " "
- }
- local typ : type `varlist'
= local typ : type varname
- if ("`typ'" == "strL" | substr("`typ'", 1, 3) == "str") {
= if ("str1227" == "strL" | substr("str1227", 1, 3) == "str") {
- NoHexadecimal `hexadecimal'
= NoHexadecimal
-------------------------------------------------------------------------------------------- begin levelsof.NoHexadecimal ---
- if (`"`0'"' == "") {
= if (`""' == "") {
- exit
---------------------------------------------------------------------------------------------- end levelsof.NoHexadecimal ---
- NoMatrow `matrow'
= NoMatrow
------------------------------------------------------------------------------------------------- begin levelsof.NoMatrow ---
- if (`"`0'"' == "") {
= if (`""' == "") {
- exit
--------------------------------------------------------------------------------------------------- end levelsof.NoMatrow ---
- }
- mata: st_rclear()
- if ("`typ'" == "strL") {
= if ("str1227" == "strL") {
LevelsOfStrL `varlist' if `touse', separate(`"`separate'"') `clean' matcell(`matcell')
}
- else if (substr("`typ'", 1, 3) == "str") {
= else if (substr("str1227", 1, 3) == "str") {
- local isclean = ("`clean'" != "")
= local isclean = ("" != "")
- mata: LevelsOfString("`varlist'", "`touse'", `"`separate'"', `isclean', "`matcell'")
= mata: LevelsOfString("varname", "__000000", `" "', 0, "")
- }
- else {
local isint = inlist("`typ'", "byte", "int", "long")
mata: LevelsOfReal("`varlist'", "`touse'", `"`separate'"', `isint', 1, "`matrow'", "`matcell'", "`hexadecimal'" != "")
if ("`usetab'" == "usetab") {
cap LevelsOfTab `varlist' if `touse', `missing' separate(`"`separate'"') matrow(`matrow') matcell(`matcell') `hexadecimal'
if (_rc) {
mata: LevelsOfReal("`varlist'", "`touse'", `"`separate'"', `isint', 0, "`matrow'", "`matcell'", "`hexadecimal'" != "")
}
}
}
- if ("`local'" != "") {
= if ("C5" != "") {
- c_local `local' `"`r(levels)'"'
= c_local C5 `""'
- }
- di as text `"`r(levels)'"'
= di as text `""'
-------------------------------------------------------------------------------------------------------------- end levelsof
Code:
- levelsof varname if strpos(varname, "Avez-vous un agrément pour votre activité secondaire ?") > 0 & strpos(varname, "Avez-vous
> un agrément pour votre activité secondaire ? [Autre]") == 0, local(A23)
------------------------------------------------------------------------------------------------------------ begin levelsof ---
- version 15.0
- syntax varname [if] [in] [, Separate(str) MISSing Local(name local) Clean MATROW(name) MATCELL(name) HEXadecimal ]
- if ("`missing'" == "") {
= if ("" == "") {
- marksample touse, strok
- }
- else {
marksample touse, strok novarlist
}
- if (`"`separate'"' == "") {
= if (`""' == "") {
- local separate " "
- }
- local typ : type `varlist'
= local typ : type varname
- if ("`typ'" == "strL" | substr("`typ'", 1, 3) == "str") {
= if ("str1227" == "strL" | substr("str1227", 1, 3) == "str") {
- NoHexadecimal `hexadecimal'
= NoHexadecimal
-------------------------------------------------------------------------------------------- begin levelsof.NoHexadecimal ---
- if (`"`0'"' == "") {
= if (`""' == "") {
- exit
---------------------------------------------------------------------------------------------- end levelsof.NoHexadecimal ---
- NoMatrow `matrow'
= NoMatrow
------------------------------------------------------------------------------------------------- begin levelsof.NoMatrow ---
- if (`"`0'"' == "") {
= if (`""' == "") {
- exit
--------------------------------------------------------------------------------------------------- end levelsof.NoMatrow ---
- }
- mata: st_rclear()
- if ("`typ'" == "strL") {
= if ("str1227" == "strL") {
LevelsOfStrL `varlist' if `touse', separate(`"`separate'"') `clean' matcell(`matcell')
}
- else if (substr("`typ'", 1, 3) == "str") {
= else if (substr("str1227", 1, 3) == "str") {
- local isclean = ("`clean'" != "")
= local isclean = ("" != "")
- mata: LevelsOfString("`varlist'", "`touse'", `"`separate'"', `isclean', "`matcell'")
= mata: LevelsOfString("varname", "__000000", `" "', 0, "")
- }
- else {
local isint = inlist("`typ'", "byte", "int", "long")
mata: LevelsOfReal("`varlist'", "`touse'", `"`separate'"', `isint', 1, "`matrow'", "`matcell'", "`hexadecimal'" != "")
if ("`usetab'" == "usetab") {
cap LevelsOfTab `varlist' if `touse', `missing' separate(`"`separate'"') matrow(`matrow') matcell(`matcell') `hexadecimal'
if (_rc) {
mata: LevelsOfReal("`varlist'", "`touse'", `"`separate'"', `isint', 0, "`matrow'", "`matcell'", "`hexadecimal'" != "")
}
}
}
- if ("`local'" != "") {
= if ("A23" != "") {
- c_local `local' `"`r(levels)'"'
= c_local A23 `"`"Avez-vous un agrément pour votre activité secondaire ?"'"'
- }
- di as text `"`r(levels)'"'
= di as text `"`"Avez-vous un agrément pour votre activité secondaire ?"'"'
`"Avez-vous un agrément pour votre activité secondaire ?"'
-------------------------------------------------------------------------------------------------------------- end levelsof

Comment