自定义程序 - 调试语法

时间:2014-01-16 21:59:49

标签: stata

我创建了以下程序,该程序不能很好地处理字符串表达式。我无法找到正确的调整来添加我的语法定义,以使其按预期工作。

我认为这很小,但我还没能做到。或者,也可以理解对有用的东西的引用。

包括程序和一些产生相同错误的虚拟代码。

谢谢!

cap program drop repl_conf
program define repl_conf
    syntax varlist =exp  [if] 
    qui count `if'
    if r(N) ==0 {
    di as err "NO MATCHES -- NO REPLACE"
    exit 9
    }
    else {
    noi dis "SUCCESSFUL REPLACE of >=1 OBS -- " r(N) " OBS replaced"
    qui replace `varlist' `exp' `if'
    }
end

sysuse auto, clear
repl_conf length=999 if length==233
repl_conf make="ZZZ" if make=="AMC Concord"
type mismatch
r(109);

1 个答案:

答案 0 :(得分:3)

这进一步发展。我移动了第二条消息,以便仅在replace成功时才会发出。

program define repl_conf
    gettoken varname 0 : 0, parse(=) 
    confirm var `varname' 
    gettoken eq 0 : 0, parse(=) 
    syntax anything [if] 
    qui count `if'
    if r(N) == 0 {
         di as err "NO MATCHES -- NO REPLACE"
         exit 9
    }
    else {
         qui replace `varname' = `anything' `if'
         noi di "SUCCESSFUL REPLACE of >=1 OBS -- " r(N) " OBS replaced"
    }
end

sysuse auto, clear
repl_conf length=999 if length==233
repl_conf make="ZZZ" if make=="AMC Concord"