楼主: lirui1980
10338 7

[原创博文] [求助]如何将字符型变量转换为日期型变量 [推广有奖]

  • 0关注
  • 0粉丝

小学生

71%

还不是VIP/贵宾

-

威望
0
论坛币
46 个
通用积分
0
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
118 点
帖子
8
精华
0
在线时间
0 小时
注册时间
2008-10-16
最后登录
2009-2-7

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
我从CSMAR数据库下载了日期数据,但是导入到SAS后,全是字符型的,我想将该变量转换为日期型,请问应该如何做?谢谢
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:字符型变量 变量转换 日期型 字符型 CSMAR

回帖推荐

llulla 发表于3楼  查看完整内容

I don't think it works using the SAS code above. Please see the following SAS code. The hospitalization date is characteristic variable in the data set a as HOSPADMIT, such as '19991103'. It means in the year '1999', 'NOV.', and date  '03' The hospitalization date should be transferred to '03NOV1999' in the data set b as variable of HOSP_ADMIT_DT. Please try it.Data b;Set ...

宜桦 发表于2楼  查看完整内容

how does your data look like? suppose it is ddmmyy10.new_var = input(old_var, ddmmyy10.);

本帖被以下文库推荐

沙发
宜桦 发表于 2008-10-24 21:57:00 |只看作者 |坛友微信交流群

how does your data look like? suppose it is ddmmyy10.

new_var = input(old_var, ddmmyy10.);

已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 3  论坛币 + 3   查看全部评分

使用道具

藤椅
llulla 发表于 2008-10-25 00:44:00 |只看作者 |坛友微信交流群

I don't think it works using the SAS code above. Please see the following SAS code.

The hospitalization date is characteristic variable in the data set a as HOSPADMIT, such as '19991103'.

It means in the year '1999', 'NOV.', and date  '03' The hospitalization date should be transferred to '03NOV1999'

in the data set b as variable of HOSP_ADMIT_DT.

Please try it.

Data b;

Set a;

HOSP_ADMIT_YYYY = substr(HOSPADMIT, 1,4);

HOSP_ADMIT_MM = substr(HOSPADMIT, 5,2);

HOSP_ADMIT_DD = substr(HOSPADMIT, 7,2);

HOSP_ADMIT_DT = MDY(HOSP_ADMIT_MM, HOSP_ADMIT_DD, HOSP_ADMIT_YYYY);

format HOSP_ADMIT_DT date9.;

DROP HOSPADMIT HOSP_ADMIT_YYYY HOSP_ADMIT_MM HOSP_ADMIT_DD;

Run;

 

[此贴子已经被作者于2008-10-25 1:05:20编辑过]

已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

总评分: 经验 + 3  论坛币 + 3   查看全部评分

使用道具

板凳
宜桦 发表于 2008-10-25 02:09:00 |只看作者 |坛友微信交流群

in that case then

new_var = input(old_var, yymmdd8.);

I made an assumption at the first place. Yes, you are right, It will depend on how the format the old variable looks like.

In your code, the YYYY, MM and DD variables are still character variables. you have to use input function to transpose.

[此贴子已经被作者于2008-10-25 2:14:00编辑过]

使用道具

报纸
llulla 发表于 2008-10-25 06:42:00 |只看作者 |坛友微信交流群

Thank you for your reply. In that case, your code is right.

Your code uses the function of "INPUT". My code uses the function of "MDY". My understanding, in some cases, the INPUT function is easy to manipulate, to transfer one characteristic variable to numeric or date variable using argument option.

In some cases, if the date, month, and year are separated to different characteristic variables, the MDY function could combined them together and transfer it to date variable.

[此贴子已经被作者于2008-10-25 6:44:05编辑过]

使用道具

地板
lirui1980 发表于 2008-10-25 13:12:00 |只看作者 |坛友微信交流群
感谢楼上二位

使用道具

7
idiele 发表于 2012-5-22 17:29:39 |只看作者 |坛友微信交流群
i have dealt with the problem all the time. and i think there is trick if you are not willing to deal with SAS code. that is, you can change the cell format for the date variable in the excel to '14/03/2001' which can be recognized by SAS and will be transferred to '14MAR2001' (date9.) in SAS.

使用道具

8
hellofuture 发表于 2013-4-18 20:11:14 |只看作者 |坛友微信交流群
楼主你好,请问你的问题解决了吗?我面临和你一样的问题,但楼上两位的解决办法好像不行啊?麻烦您指教下,谢谢~

使用道具

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

本版微信群
加好友,备注cda
拉您进交流群

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

GMT+8, 2024-4-28 02:32