将数字日期变量转换为字符串

时间:2018-05-24 22:05:26

标签: stata

我想执行previous question中描述的相反转换。

我尝试过以下代码:

decode numeric, gen(string)

然而,它给了我一个错误:

numeric not labeled
r(182);

是否可以进行此类转换?

1 个答案:

答案 0 :(得分:2)

是的,但只要数字变量缺少值标签,您就需要使用string()函数:

. clear

input str10 string
"06-08-2003"
"19-11-1923"
"12-04-1997"
"29-12-1945" 
end

. generate numeric = daily(string, "DMY")
. format %tddd-NN-CCYY numeric

. generate new_string = string(numeric, "%tddd-NN-CCYY")

. list

   +--------------------------------------+
   |     string      numeric   new_string |
   |--------------------------------------|
1. | 06-08-2003    6-08-2003    6-08-2003 |
2. | 19-11-1923   19-11-1923   19-11-1923 |
3. | 12-04-1997   12-04-1997   12-04-1997 |
4. | 29-12-1945   29-12-1945   29-12-1945 |
   +--------------------------------------+
相关问题