Hi all,
I have a dataset. Each variable is in a matched set of "three" with a grade, cat, and adevent... which have the same numberings (like cat1, grade1, adevent1) and endings (like _scr or _fu1)
I'm trying to drop all three matching variables every time all the grades for that group are 0. My approach has been extracting the necessary parts of the grade string to create matching adevent and cat strings.
All the code works except for the last two lines:
if r(N) == 6 drop adevent`val'`when'
if r(N) == 6 drop cat`val'`when'
Howver, the grade part will drop... but the associated cat and adevent parts refuse to drop.
Any suggestions how to correct this?
I have a dataset. Each variable is in a matched set of "three" with a grade, cat, and adevent... which have the same numberings (like cat1, grade1, adevent1) and endings (like _scr or _fu1)
Code:
* Example generated by -dataex-. For more info, type help dataex clear input byte(cat1_scr grade1_scr) str2 adevent1_scr byte(cat2_scr grade2_scr) str2 adevent2_scr byte(cat6_wk4 grade6_wk4) str24 adevent6_wk4 byte(cat7_maint1 grade7_maint1) str43 adevent7_maint1 byte(cat8_maint1 grade8_maint1) str43 adevent8_maint1 0 0 "" 0 0 "" 0 0 "" 0 0 "" 0 0 "" 0 1 "" 0 0 "" 0 0 "" 0 0 "" 0 0 "" 0 0 "" 0 0 "" 0 0 "" 0 0 "" 0 0 "" 0 0 "" 0 0 "" 0 0 "" 0 0 "" 0 0 "" 0 0 "" 0 0 "" 0 0 "" 0 0 "" 0 1 "" 0 0 "" 0 0 "" 0 0 "" 0 0 "" 0 0 "" end label values cat1_scr cat_ label values cat2_scr cat_ label values grade1_scr grade_ label values grade2_scr grade_ label def grade_ 0 "0", modify label def grade_ 1 "1", modify label values cat6_wk4 cat4_ label values grade6_wk4 grade4_ label def grade4_ 0 "0", modify label values cat7_maint1 cat7_ label values cat8_maint1 cat7_ label values grade7_maint1 grade7_ label values grade8_maint1 grade7_ label def grade7_ 0 "0", modify label def grade7_ 1 "1", modify
Code:
* drop matching cat and adevent if all grades == 0 qui desc local c = r(N) foreach var of varlist grade* { quietly count if `var' == 0 * get part of string after and including the "_" (e.g. _wk1) local when = substr("`var'", strpos("`var'", "_") , .) * get part of string before the "_" (e.g., grade7) local p = substr("`var'", 1, strpos("`var'", "_") - 1) * and get just the numeric value out of it local val = ustrregexra("`p'","[^0-9]+","") * drop grade, cat, and adevent of same 'type' if 'grade' is all 0s if r(N) == 6 drop `var' if r(N) == 6 drop adevent`val'`when' if r(N) == 6 drop cat`val'`when' }
if r(N) == 6 drop adevent`val'`when'
if r(N) == 6 drop cat`val'`when'
Howver, the grade part will drop... but the associated cat and adevent parts refuse to drop.
Any suggestions how to correct this?
Comment