楼主: eblog
11589 43

[编程问题求助] Stata编程(讨论4) [推广有奖]

21楼
sungmoo 发表于 2010-2-1 11:54:04
jzhyue 发表于 2010-2-1 11:43 感觉有时候syntax varlist,选项的格式更规范
stata的说法:anything is for use when you want the command to have the nearly standard syntax of command name followed by something followed by if, in, options, etc. For instance, perhaps the command is to be followed by an expression or expressions, or a list of numbers.

22楼
eblog 发表于 2010-2-1 12:09:16
以下代码应该可以比较方便的控制参数为numeric:

  syntax anything
  foreach x in `anything' {
    qui cap assert sqrt(abs(`x'))!=.
    if _rc {
      di as text _n "The input " as result "`x'" as text " not be numeric! try again please! "
      exit
    }
  }

23楼
sungmoo 发表于 2010-2-1 12:10:58
arlionn 发表于 2010-2-1 11:26
sungmoo 发表于 2010-2-1 11:11
这里有一个困难在于:若用syntax anything,anything其实也可以包括variable。
这个可以用
local type: type `1'
if "`type'" != "numeric"{
  ……
}
这样的语句控制一下。
似乎不行。以下会出错:

loc x=1
local type: type `x'
n di  "`type'"

24楼
sungmoo 发表于 2010-2-1 12:12:45
eblog 发表于 2010-2-1 12:09 以下代码应该可以比较方便的控制参数为numeric
问题是,若输入者不小心输入了(数值型)变量名呢?

(mytest a b,a与b都是已打开的数据库的数值型变量)

25楼
eblog 发表于 2010-2-1 12:14:38
sungmoo 发表于 2010-2-1 12:12
eblog 发表于 2010-2-1 12:09 以下代码应该可以比较方便的控制参数为numeric
问题是,若输入者不小心输入了(数值型)变量名呢?

(mytest a b,a与b都是已打开的数据库的数值型变量)
程序前面的 clear all 已经清空数据库了,
另外,变量名不能为纯粹的“数值型”

26楼
sungmoo 发表于 2010-2-1 12:15:31
arlionn 发表于 2010-2-1 11:26 这个可以用
local type: type `1'
if "`type'" != "numeric"{
  ……
}
这样的语句控制一下。
loc macro: type后面须接变量名吧?

27楼
sungmoo 发表于 2010-2-1 12:21:32
eblog 发表于 2010-2-1 12:09 以下代码应该可以比较方便的控制参数为numeric:
syntax anything
foreach x in `anything' {
  qui cap assert sqrt(abs(`x'))!=.
  if _rc {
    di as text _n "The input " as result "`x'" as text " not be numeric! try again please! "
    exit
  }
}
……
syntax anything
if indexnot("`anything'"," 0123456789"){
  di as error "Only 0-9 and space can be input"
exit
}
……

28楼
eblog 发表于 2010-2-1 12:27:23
sungmoo 发表于 2010-2-1 12:21
eblog 发表于 2010-2-1 12:09 以下代码应该可以比较方便的控制参数为numeric:
syntax anything
foreach x in `anything' {
  qui cap assert sqrt(abs(`x'))!=.
  if _rc {
    di as text _n "The input " as result "`x'" as text " not be numeric! try again please! "
    exit
  }
}
……
syntax anything
if indexnot("`anything'"," 0123456789"){
  di as error "Only 0-9 and space can be input"
exit
}
……
还应该再加上小数点和负号

29楼
sungmoo 发表于 2010-2-1 12:41:14
eblog 发表于 2010-2-1 12:27 还应该再加上小数点和负号
上面写法是服从你的程序写的。(所以列出“只可以是什么”)

另:若单纯检查是否是实数,用indexnot还实现不了。

30楼
sungmoo 发表于 2010-2-1 12:47:49
eblog 发表于 2010-2-1 12:09 qui cap assert sqrt(abs(`x'))!=.
只用abs()即可。

(missing value(.)不算numeric)

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-10-28 11:51