Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: RE: Bug with destring or operator error?


From   "Ben Hoen" <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: Bug with destring or operator error?
Date   Thu, 22 Aug 2013 14:26:34 -0400

*So I added the following before the destring and it solved the problem!
desc // the variable __000001 is listed there.  
drop __000001 //this did drop this variable

*and then destring worked fine.

*without the drop command it still hiccupped.  See trace output below
*FYI I used parmest (via SSC) to create the bgreg.dta dataset
* I have no idea what that variable would have been it is not shown in the
regression output.
* in the dataset it is located between the p-value and the lower CI


*Here is the trace output
*=========================BEGIN =======================

. set trace on

. destring blkgrp, replace force
 
----------------------------------------------------------------------------
----------------------------- begin destring ---
  - version 7.0
  - syntax [varlist], [Generate(string) replace] [force] [float]
[Ignore(string)] [percent] [dpcomma]
  - if "`percent'" == "percent" {
  = if "" == "percent" {
    if !index(`"`ignore'"', "%") {
    local ignore `"`ignore'%"'
    }
    }
  - if "`generate'" != "" & "`replace'" != "" {
  = if "" != "" & "replace" != "" {
    di as err "options generate and replace are mutually exclusive"
    exit 198
    }
  - if "`generate'" == "" & "`replace'" == "" {
  = if "" == "" & "replace" == "" {
    di as err "must specify either generate or replace option"
    exit 198
    }
  - if "`generate'" != "" {
  = if "" != "" {
    local ct1: word count `varlist'
    local save "`varlist'"
    local 0 "`generate'"
    capture syntax newvarlist
    if _rc {
    di as err "generate(newvarlist) invalid"
    exit _rc
    }
    local generate "`varlist'"
    local varlist "`save'"
    local ct2: word count `generate'
    if `ct1' != `ct2' {
    di as err "number of variables in varlist must equal"
    di as err "number of variables in generate(newvarlist)"
    exit 198
    }
    }
  - local m 1
  - if `"`ignore'"' == "" {
  = if `""' == "" {
  - local ignore ""
  - }
  - local l = length(`"`ignore'"')
  = local l = length(`""')
  - while `m' <= `l' {
  = while 1 <= 0 {
    local char`m' = substr(`"`ignore'"', `m', 1)
    if substr(`"`ignore'"', `m', 1) == " " {
    local char`m' " "
    }
    local m = `m' + 1
    }
  - if "`generate'" != "" {
  = if "" != "" {
    tokenize `varlist'
    local flag 0
    local jj 1
    local yy 1
    local varno 0
    while "`1'" != "" {
    local varno = `varno' + 1
    capture confirm string variable `1'
    if _rc != 0 {
    di as txt "`1' already numeric; no " as res "generate"
    local newvar : word `varno' of `generate'
    mac shift
    }
    else {
    tempvar temp
    qui gen str1 `temp' = ""
    qui replace `temp' = `1'
    qui compress `temp'
    while `"`char`jj''"' != "" {
    local t `"`char`jj''"'
    qui replace `temp' = subinstr(`temp', `"`t'"', "", .)
    local jj = `jj' + 1
    }
    if ("`dpcomma'" != "") {
    qui replace `temp' = subinstr(`temp', `","', ".", 1)
    }
    qui replace `temp' = trim(`temp')
    qui count if `temp'=="" | `temp'=="." | (length(`temp')==2 &
inrange(`temp',".a",".z"))
    local r = r(N)
    qui count if real(`temp') >= .
    local s = r(N)
    if `r' != `s' {
    local flag 1
    }
    if `flag' == 1 & "`force'" == "" {
    if `"`ignore'"' != "" {
    di as txt "`1' contains " "characters not specified in " as res
"ignore()" as txt "; no " as res "generate"
    }
    else {
    di as txt "`1' contains " "nonnumeric characters; no " as res "generate"
    }
    local flag 0
    local jj 1
    mac shift
    }
    else {
    tempvar OLDVAR ind con
    qui gen str1 `OLDVAR' = ""
    qui replace `OLDVAR' = `1'
    qui gen byte `ind' = .
    qui gen byte `con' = 0
    while `"`char`yy''"' != "" {
    local t `"`char`yy''"'
    qui replace `ind' = 1 if index(`OLDVAR', `"`t'"') != 0
    qui count if `ind' == 1
    if r(N) > 0 {
    if `"`t'"' == " " {
    local b `"`b' space"'
    }
    else {
    local b `"`b' `t'"'
    }
    }
    qui replace `ind' = .
    qui replace `con' = 1 if `"`t'"' == "%" & index(`OLDVAR', `"`t'"') != 0
    qui replace `OLDVAR' = subinstr(`OLDVAR', `"`t'"', "", .)
    local yy = `yy' + 1
    }
    if ("`dpcomma'" != "") {
    qui replace `OLDVAR' = subinstr(`OLDVAR', `","', ".", 1)
    }
    local c Characters removed were: `b'
    local newvar : word `varno' of `generate'
    local vl: variable label `1'
    if "`float'" == "" {
    qui gen double `newvar' = real(`OLDVAR')
    }
    else {
    qui gen float `newvar' = real(`OLDVAR')
    }
    move `newvar' `1'
    move `1' `newvar'
    Charcopy `1' `newvar'
    label variable `newvar' `"`vl'"'
    char `newvar'[destring] `c'
    qui count if `con' == 1
    if "`percent'" != "" & r(N) > 0 {
    qui replace `newvar' = `newvar'/100
    }
    qui compress `newvar'
    local type : type `newvar'
    if "`force'" != "" {
    di as txt "`1' contains nonnumeric " "characters; `newvar' " as res
"generated " as txt "as " as res "`type'"
    }
    if `"`b'"' != "" {
    di as txt "`1': characters" as res `"`b'"' as txt " removed; `newvar' "
as res "generated " as txt "as " as res "`type'"
    }
    else if `"`b'"' == "" & "`force'" == "" {
    di as txt "`1' has all " "characters numeric; `newvar' " as res
"generated " as txt "as " as res "`type'"
    }
    if `s' != 0 {
    local valmsg = cond(`s' > 1, "values", "value")
    di as txt "(`s' missing `valmsg' generated)"
    }
    local b ""
    local c ""
    drop `ind' `con' `OLDVAR'
    local jj 1
    local yy 1
    mac shift
    }
    drop `temp'
    }
    }
    }
  - else if "`replace'" != "" {
  = else if "replace" != "" {
  - tokenize `varlist'
  = tokenize blkgrp
  - tempvar ind con
  - qui gen byte `con' = 0
  = qui gen byte __000001 = 0
__000001 already defined
    qui gen byte `ind' = .
    local flag 0
    local yy 1
    local jj 1
    while "`1'" != "" {
    capture confirm string variable `1'
    if _rc != 0 {
    di as txt "`1' already numeric; no " as res "replace"
    mac shift
    }
    else {
    tempvar temp
    qui gen str1 `temp' = ""
    qui replace `temp' = `1'
    qui compress `temp'
    while `"`char`jj''"' != "" {
    local t `"`char`jj''"'
    qui replace `temp' = subinstr(`temp', `"`t'"', "", .)
    local jj = `jj' + 1
    }
    if ("`dpcomma'" != "") {
    qui replace `temp' = subinstr(`temp', `","', ".", 1)
    }
    qui replace `temp' = trim(`temp')
    qui count if `temp'=="" | `temp'=="." | (length(`temp')==2 &
inrange(`temp',".a",".z"))
    local r = r(N)
    qui count if real(`temp') >= .
    local s = r(N)
    if `r' != `s' {
    local flag 1
    }
    if `flag' == 1 & "`force'" == "" {
    if `"`ignore'"' != "" {
    di as txt "`1' contains " "characters not specified in " as res
"ignore()" as txt "; no " as res "replace"
    }
    else {
    di as txt "`1' contains " "nonnumeric characters; no " as res "replace"
    }
    local flag 0
    local jj 1
    mac shift
    }
    else {
    while `"`char`yy''"' != "" {
    local t `"`char`yy''"'
    qui replace `ind' = 1 if index(`1', `"`t'"')!=0
    qui count if `ind' == 1
    if `"`t'"' == " " & r(N) > 0 {
    local b `"`b' space"'
    }
    if r(N) > 0 {
    local b `"`b' `t'"'
    }
    qui replace `ind' = .
    qui replace `con' = 1 if `"`t'"' == "%" & index(`1', `"`t'"') != 0
    qui replace `1' = subinstr(`1', `"`t'"', "", .)
    local yy = `yy' + 1
    }
    if ("`dpcomma'" != "") {
    qui replace `1' = subinstr(`1', `","', ".", 1)
    }
    local c Characters removed were: `b'
    tempvar switch
    local type = cond("`float'" == "float", "float", "double")
    qui gen `type' `switch' = real(`1')
    char rename `1' `switch'
    move `switch' `1'
    local vl: variable label `1'
    drop `1'
    if "`float'" == "" {
    qui gen double `1' = `switch'
    }
    else {
    qui gen float `1' = `switch'
    }
    move `1' `switch'
    label variable `1' `"`vl'"'
    char rename `switch' `1'
    char `1'[destring] `c'
    qui count if `con' == 1
    if "`percent'" != "" & r(N) != 0 {
    qui replace `1' = `1'/100
    }
    qui replace `con' = 0
    qui compress `1'
    local type : type `1'
    if "`force'" != "" {
    di as txt "`1' contains nonnumeric " "characters; " as res "replaced "
as txt "as " as res "`type'"
    }
    if `"`b'"' != "" {
    di as txt "`1'"": characters" as res `"`b'"' as txt " removed; " as res
"replaced " as txt "as " as res "`type'"
    }
    else if `"`b'"' == "" & "`force'" == "" {
    di as txt "`1' has all " "characters numeric; " as res "replaced " as
txt "as " as res "`type'"
    }
    if `s' != 0 {
    local valmsg = cond(`s' > 1, "values", "value")
    di as txt "(`s' missing `valmsg' generated)"
    }
    local b ""
    local c ""
    local jj 1
    local yy 1
    drop `switch'
    mac shift
    }
    drop `temp'
    }
    }
    }
 
----------------------------------------------------------------------------
------------------------------- end destring ---
r(110);

end of do-file

r(110);


*=========================END==========================


Ben Hoen
LBNL
Office: 845-758-1896
Cell: 718-812-7589


-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Joe Canner
Sent: Thursday, August 22, 2013 2:10 PM
To: [email protected]
Subject: st: RE: Bug with destring or operator error?

__00001 sounds like Stata's internal designation of a temporary variable.

-destring- is an .ado file, so you can -set trace on- before the -destring-
and see exactly where the problem is.  Let us know what you find and maybe
we can dig around in the -destring- code and see what is causing it.

Regards,
Joe Canner


-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Ben Hoen
Sent: Thursday, August 22, 2013 1:49 PM
To: [email protected]
Subject: st: Bug with destring or operator error?

Hi all,

I am having trouble with destring in an ado.  When I run the ado I get the
following error:


. destring blkgrp, g(bgtemp) force
__000001 already defined
r(110);

end of do-file

r(110);

But after I get this error, if I just run the destring command again, it
works fine:
. destring blkgrp, g(bgtemp) force
blkgrp contains nonnumeric characters; bgtemp generated as int
(44 missing values generated)


I have tried a number of different workarounds to keep the error from
happening but to no avail.  Obvioulsy the point is to avoid manually coming
and rerunning the second half of the ado.  Any ideas?  

Here is the full ado: 

*=============== START ==============
cd "B:\OtherDatabase\pv_prop_2\data\regest"
use bgreg.dta, clear //to call up the data g blkgrp=substr(parm,4,.) if
substr(parm,1,3)=="pv_"  // the extract the number from the string destring
blkgrp, replace force

/*
I also tried the following variations

With or without .replace blkgrp=trim(blkgrp) before destring

Instead use
destring blkgrp, g(bgtemp) force
drop blkgrp
rename bgtemp blkgrp
*/
*========== END ==============

Ben Hoen
Staff Research Associate
Lawrence Berkeley National Laboratory
Office: 845-758-1896
Cell: 718-812-7589
[email protected]
http://emp.lbl.gov/staff/ben-hoen

Visit our publications at: 
http://emp.lbl.gov/reports/re

Sign up for our email list to receive publication notifications at: 
https://spreadsheets.google.com/a/lbl.gov/spreadsheet/viewform?formkey=dGlFS
1U1NFlUNzQ1TlBHSzY2VGZuN1E6MQ






*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index