楼主: windlove
3536 8

hybrid format in the dataset, How to input into SAS [推广有奖]

  • 3关注
  • 2粉丝

已卖:1195份资源

副教授

37%

还不是VIP/贵宾

-

威望
0
论坛币
1562 个
通用积分
55.4072
学术水平
21 点
热心指数
12 点
信用等级
6 点
经验
972 点
帖子
305
精华
0
在线时间
1275 小时
注册时间
2006-3-15
最后登录
2025-6-10

楼主
windlove 发表于 2009-11-10 09:02:38 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币
I have the dataset with a column of DATE. However, my supervisor arranged the file from different sources. So the column of DATE in the excel file are not all in DATE formate. eg.

There are both 25/06/2001, and 20000312 in that column, which the first is DATE format, but the second is recognized as numerical value. It's fairly easy to input just with either of them. But how to input both types of DATE presenting in the same column?

Thank you very much.

And i want convert both to the format of yymmddn8., ie(20000312, this is data format in SAS, but not in EXCEL of my original data file.).
二维码

扫码加我 拉你入群

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

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

关键词:dataset format Hybrid DataS FORMA SAS How format Hybrid Input

回帖推荐

cata1234 发表于5楼  查看完整内容

try this one: data test; input a $20.; if length (trim(a)) = 10 then do; year = substr(trim(a),7,4); month= substr(trim(a),4,2); day = substr(trim(a),1,2); b = mdy(month,day,year); end; if length(trim(a)) = 8 then do; year = substr(trim(a),1,4); month= substr(trim(a),5,2); day = substr(trim(a),7,2); b=mdy(month,day,year); end; cards; 25/05/2001 20 ...

jingju11 发表于6楼  查看完整内容

4# windlove 问题哪有你描述的那么复杂?SAS仅仅有关date的formats 就有几十个。

本帖被以下文库推荐

沙发
metamatics 发表于 2009-11-10 09:28:35
if a is date, do nothing
if a is numeric, transform
simple if then statement

藤椅
windlove 发表于 2009-11-10 09:35:21
yes offcoz... I wanna know what code i can use to determine if it is in a date format or it's a numerical value.

because SAS only have formats of Char and numerical, and DATE is a numerical value as well.

板凳
windlove 发表于 2009-11-10 09:35:37
yes offcoz... I wanna know what code i can use to determine if it is in a date format or it's a numerical value.

because SAS only have formats of Char and numerical, and DATE is a numerical value as well.

报纸
cata1234 发表于 2009-11-10 12:42:53
try this one:

data test;
input a $20.;
if length (trim(a)) = 10 then do;
   year = substr(trim(a),7,4);
   month= substr(trim(a),4,2);
   day  = substr(trim(a),1,2);
   b = mdy(month,day,year);
   end;
if length(trim(a)) = 8 then do;
   year = substr(trim(a),1,4);
   month= substr(trim(a),5,2);
   day  = substr(trim(a),7,2);
   b=mdy(month,day,year);
   end;
cards;
25/05/2001
20000312
19990909
12/01/2009
;
run;
proc print data = test;
var b;
format b yymmdd8.;
run;

output:
Obs           b

                                          1     01-05-25
                                          2     00-03-12
                                          3     99-09-09
                                          4     09-01-12
已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

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

地板
jingju11 发表于 2009-11-10 21:54:14
4# windlove

  1. data s;
  2. input date  $10.;
  3. if find(date,'/') then dat =input(date,ddmmyy10.);
  4.    else dat =input(date, yymmdd10.);
  5. format dat yymmdd8.;
  6. datalines;
  7. 20010309
  8. 10/03/2006
  9. 3/05/2000
  10. ;
  11. run;
  12. proc print data =s;run;
复制代码
问题哪有你描述的那么复杂?
because SAS only have formats of Char and numerical...,
SAS仅仅有关date的formats 就有几十个。
已有 1 人评分经验 论坛币 收起 理由
bakoll + 3 + 3 精彩帖子

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

7
windlove 发表于 2009-11-11 11:40:18
Thanks for all replies... Those programs all worked.. But my question is how to find a generalized way to input  hybrid date form. ‘/' is just a particular case. Also like "-" ,  01032000, 20000103, 2000-01-03, etc.  I have hundreds groups of data, writing a Macro for input date to "find" these case is not practical.

8
jingju11 发表于 2009-11-11 21:42:12
7# windlove


  1. 01032000...
复制代码


你能告诉我这个日期是什么吗?你说是Jan03, 2000, 我说是Mar01, 2000。反之亦然。我们尚且无法确定这到底是什么,何况SAS。
我不知道你的问题是否只是你的设想。如果真是如此,也没有很多的办法好像。我想,一般的数据不会乱到如此的程度,data entry也有规则,不能随心所欲的。
还是一点,你可以让SAS读不同的格式,但是万一某个读错了,像以上的例子,你还会采用它吗?

9
bobguy 发表于 2009-11-18 10:48:51
It is much easier to use correct informat.

Here is an example.

58   data _null_;
59   informat datevar anydtdte10.;
60   infile cards;
61   input datevar;
62   list;
63   put datevar= mmddyy10. ///;
64   cards;

datevar=06/25/2001
RULE:      ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----
65         25/06/2001



datevar=06/25/2001
66         25-06-2001



datevar=06/25/2001
67         2001-6-25



datevar=06/25/2001
68         20010625
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


69   ;

HTH

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-9 16:29