楼主: jiangh_fox
9939 5

[问答] SAS 里 不同数据类型转换 [推广有奖]

  • 0关注
  • 0粉丝

小学生

0%

还不是VIP/贵宾

-

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

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
学sas 有段时间了。
但是,sas 的日期、数字、字符,三者之间的转换一直有点晕晕的。
看到例子程序明白,自己一写,就问题一堆。

哪位大侠,给指点下。
二维码

扫码加我 拉你入群

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

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

关键词:数据类型转换 类型转换 数据类型 子程序 SAS 类型转换

沙发
sas_user 发表于 2011-4-26 22:18:35 |只看作者 |坛友微信交流群
SAS的日期时间都是数字型的,以便于计算。有各种格式可以从数值转变为字符型。1960-01-01是日期的开始,SAS的日期是0. 之后每天加一。1960-01-02的值为1. 1959-12-31的值为-1. 以此类推。时间是一秒为单位计算。从0(00:00:00)到86400(24:00:00)。每加一秒数值加一。
常用的日期时间转换的function如下:
data _null_;
a=mdy(12,31,1959);
b='02jan1960'd;
c='24:00:00't;
d='00:00:00't;
e='31dec90:5:00:00'dt;
format e datetime18.;
put a= b= c= d= e=;
run;
a=-1 b=1 c=86400 d=0 e=31DEC90:05:00:00

使用道具

藤椅
qiuya 发表于 2011-4-28 07:54:45 |只看作者 |坛友微信交流群
1# jiangh_fox


I give you some example in my real project :

The Character version of Date I`m usually encountered with looks like this "mm/dd/yyyy". What I want is to transfer this into Date9. format which could be esaily computed in SAS.

Define the Format first:

  1. proc format ;
  2. value $mon
  3. "01" = "JAN"
  4. "02" = "FEB"
  5. "03" = "MAR"
  6. "04" = "APR"
  7. "05" = "MAY"
  8. "06" = "JUN"
  9. "07" = "JUL"
  10. "08" = "AUG"
  11. "09" = "SEP"
  12. "10" = "OCT"
  13. "11" = "NOV"
  14. "12" = "DEC";
  15. run;
复制代码


Set up the utility macro like this :

  1. ** This macro used for original character dates like xx/xx/xxxx ***;
  2. ** Define Length dd $2. mm $3. yy $4. at first ***;
  3. %global dt;
  4. %macro dt (indt = , outdt = );
  5. format &outdt date9.;
  6. if &indt ne '' then do;
  7. mm = put(substr(compress(&indt), 1,2), $mon.);
  8. dd = substr(compress(&indt), 4,2);
  9. yy = substr(compress(&indt), 7,4);
  10. &outdt = input(dd||mm||yy, date9.);
  11. end;
  12. else do;
  13. &outdt = .;
  14. end;
  15. %mend dt;
复制代码



After that, you could easily call macro %dt within the rest of your program.

  1. ** Calculate Age from DEMO **;
  2. data demo_ (keep = subjid INVID ageyr SEX_LONG_NM SUBRACE_24_LONG_NM rename = (SUBRACE_24_LONG_NM = race)) ;
  3. length dd $2. mm $3. yy $4. biryear $10.;
  4. merge demo (in=a) ic (in=b);
  5. by subjid;
  6. if a ;
  7. biryear = "01/07/"||substr(BIRTHDT_STR, 9, 4);
  8. %dt(indt = ICDTTM_DT, outdt = icdt);
  9. %dt(indt = biryear, outdt = birthdt);
  10. ageyr = floor((intck('month',BIRTHDT,ICDT) - (day(ICDT) < day(BIRTHDT)))/12);
  11. run;
复制代码


The date transfer issue come acrossed a lot in the clinical trial projects. I hope the above contents are making sense to you.

使用道具

板凳
aberb 发表于 2014-5-23 17:33:08 |只看作者 |坛友微信交流群
学习了

使用道具

报纸
921499429 发表于 2015-1-15 10:14:02 |只看作者 |坛友微信交流群
好精辟

使用道具

地板
king2917 发表于 2018-1-2 00:01:32 |只看作者 |坛友微信交流群
很好的分享

使用道具

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

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

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

GMT+8, 2024-4-24 22:20