stata destring
时间: 2023-08-12 16:05:36 浏览: 116
在 Stata 中,使用 `destring` 命令可以将字符串转换为数值型变量。该命令的语法如下:
```
destring varname [replace] [ignore(string)] [force]
```
其中,`varname` 指定要转换的字符串变量的名称,`replace` 表示替换原来的字符串变量,`ignore(string)` 表示忽略字符串中的某些字符,`force` 表示强制转换。下面是一些示例:
```
// 将字符串变量转换为数值型变量
destring var1, replace
// 忽略字符串中的 $ 符号
destring var2, replace ignore("$")
// 强制转换
destring var3, replace force
```
请注意,使用 `destring` 命令转换字符串变量时,必须确保字符串变量中只包含数字或数值型字符,否则可能会出现错误。
相关问题
stata split
Stata split command is used to divide a string variable into multiple separate variables based on a specified delimiter. It is useful when a single string variable contains multiple pieces of information that need to be separated into different variables for analysis or formatting purposes.
Syntax:
split varname, gen(newvarlist) [p(string)] [destring] [delimiter(string)] [generate] [parse(string)] [complete] [missing(string)] [uppercase] [lowercase] [propercase] [replace] [noempty] [force] [genprefix(string)] [drop] [keep] [sort]
Explanation:
- varname: The name of the string variable to be split.
- gen(newvarlist): The new variable(s) created from the split, separated by a space.
- p(string): Specifies a prefix to be applied to the variables created by split.
- destring: Automatically converts the new variables to numeric format if possible.
- delimiter(string): Specifies the delimiter to use to split the string variable. The default is a space.
- generate: Creates new variables for each split regardless of whether they are specified in newvarlist.
- parse(string): Specifies a regular expression to use for parsing the string variable.
- complete: Stata will stop execution if there are any missing values in the original variable.
- missing(string): Specifies a string to represent missing values.
- uppercase: Converts all new variables to uppercase.
- lowercase: Converts all new variables to lowercase.
- propercase: Converts all new variables to proper case.
- replace: Replaces any existing variables with the same name.
- noempty: Removes any empty variables created by the split.
- force: Forces the creation of new variables even if they already exist.
- genprefix(string): Specifies a prefix to be applied to all new variables created by split.
- drop: Drops the original string variable after the split.
- keep: Keeps the original string variable after the split.
- sort: Sorts the new variables alphabetically.
Stata exp not allowed
出现 "exp not allowed" 错误通常是因为你在使用 Stata 的某个命令时,数据中包含了非数值型的文本变量。Stata 中很多的命令都要求数据是数值型的,如果你的数据中包含了非数值型的变量,就会出现这个错误。
例如,如果你想对一个非数值型的变量执行数学运算,Stata 就会提示 "exp not allowed" 错误。你需要将变量转换为数值型,才能执行数学运算。你可以使用以下命令将变量转换为数值型:
```
destring varname, replace
```
其中,varname 是你要转换的变量名。这个命令会将变量转换为数值型,如果原始变量中包含了非数字字符,会自动将这些字符替换为缺失值。
如果你仍然无法解决问题,可以将你的命令和数据截图发给我,我可以更好地帮助你。