楼主: 林壑清
3388 9

急求助stata datatype converting [推广有奖]

  • 0关注
  • 0粉丝

硕士生

80%

还不是VIP/贵宾

-

威望
0
论坛币
2361 个
通用积分
0
学术水平
6 点
热心指数
5 点
信用等级
1 点
经验
692 点
帖子
120
精华
0
在线时间
232 小时
注册时间
2009-1-27
最后登录
2019-8-5

10论坛币
现在有组数列是str格式的
举个短例子就是像
Jan
Jan
Feb
Feb

Nov

Dec

这样的,

我现在想把它替换成 Jan=01, Feb=02……Dec=12的样子,

但是结果总是

. gen var1 = 01 if Month == Jan
Jan not found
r(111);

. replace Month=1 if Month == "Jan"
type mismatch
r(109);


着急求问高手问题出在哪里?要怎么做?

没什么金币,那个请别嫌少

最佳答案

蓝色 查看完整内容

insheet using "E:\program\stata\time.csv", comma clear list ********************************************** generate daytype1=date(date1,"MDY") format daytype1 %d list daytype1 gen yeartype = year(daytype1) gen monthtype=month(daytype1) gen daytype = day(daytype1) list yeartype monthtype daytype date1 des list year date1 month date0 daytype1 yeartype monthtype daytype ************** ...
关键词:converting datatype 求助stata Convert datat 求助 Stata datatype converting
沙发
蓝色 发表于 2010-11-9 21:54:39 |只看作者 |坛友微信交流群
insheet using "E:\program\stata\time.csv", comma clear
list
**********************************************
generate daytype1=date(date1,"MDY")
format daytype1 %d
list daytype1
gen yeartype = year(daytype1)
gen monthtype=month(daytype1)
gen daytype  =  day(daytype1)
list yeartype monthtype daytype  date1
des
list  year date1 month date0 daytype1 yeartype monthtype daytype
***************************************************************
generate daytype0=date(date0,"DM20Y")
format   daytype0 %d
list     daytype0  date0
gen yeartype2 = year(daytype0)
gen monthtype2=month(daytype0)
gen daytype2  =  day(daytype0)
list yeartype2 monthtype2 daytype2  date0
des

time.rar

614 Bytes

本附件包括:

  • time.csv
  • time.do

已有 1 人评分论坛币 收起 理由
dxystata + 20 好的意见建议

总评分: 论坛币 + 20   查看全部评分

使用道具

藤椅
蓝色 发表于 2010-11-9 21:58:33 |只看作者 |坛友微信交流群
这个问题不用悬赏啊
提出就有人回答吧

注意引号啊

gen month_num=1 if  Month=="Jan"


replace Month="1"  if  Month == "Jan"

使用道具

板凳
林壑清 在职认证  发表于 2010-11-9 22:29:05 |只看作者 |坛友微信交流群
还是
type mismatch
r(109);
为什么呀?好疑惑。

另,
有一列数是年份,有一列数是月份,
怎么把这两列数生成 mm/yyyy的一列数呀?

刚刚开始学习的小白,什么都不会,不好意思。
谢谢ls~

使用道具

报纸
h3327156 发表于 2010-11-10 02:42:28 |只看作者 |坛友微信交流群
2楼蓝色版主的答案是对的!
我测试过。

不过,我有个小问题,楼主要的是01还是1阿?
按2楼蓝色版主的方法,到时,Month这个变量会是string格式,并且Jan这样的字眼会变成1。
若楼主很坚持要01则在2楼蓝色版主那边的replace Month="1" if  Month == "Jan"
修改成replace Month="01"  if  Month == "Jan"

楼主后来问的问题,
年份是string吗? 月份是string吗?
若是,则利用gen指令即可。
string是可以相加的。
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
h894510055 + 1 + 1 + 1 谢谢楼主的鼓励,你是好人!

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

使用道具

地板
林壑清 在职认证  发表于 2010-11-10 17:51:46 |只看作者 |坛友微信交流群
1或者01都可以。我的Month这列数是红色的,datatype是str4,format是%9s,完全不明白是啥意思。
我是把版主的命令直接复制粘贴到command里用的,可是结果总告诉我说
type mismatch
r(109);
搞的我完全不明所以。

第二个问题,年份是int,月份是byte,
使用destring & replace命令,说year already numeric; no replace……

ToT 到底是怎么回事啊…

使用道具

7
蓝色 发表于 2010-11-10 18:22:58 |只看作者 |坛友微信交流群
林壑清 发表于 2010-11-10 17:51
1或者01都可以。我的Month这列数是红色的,datatype是str4,format是%9s,完全不明白是啥意思。
我是把版主的命令直接复制粘贴到command里用的,可是结果总告诉我说
type mismatch
r(109);
搞的我完全不明所以。

第二个问题,年份是int,月份是byte,
使用destring & replace命令,说year already numeric; no replace……

ToT 到底是怎么回事啊…
你把你的数据截取出10几个样本
传到网上。这样才知道你到底是哪方面的问题

使用道具

8
林壑清 在职认证  发表于 2010-11-10 18:58:04 |只看作者 |坛友微信交流群
这个,谢谢版主!!内牛满面~

Small Sample .rar

548 Bytes

本附件包括:

  • Small Sample .dta

使用道具

9
蓝色 发表于 2010-11-10 21:07:39 |只看作者 |坛友微信交流群
看看stata的手册的 stata user‘s guide上面有一章讲时间数据转换的

使用道具

10
林壑清 在职认证  发表于 2010-11-22 23:14:50 |只看作者 |坛友微信交流群
谢谢版主!

使用道具

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

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-26 16:40