2014-09-29 44 views
1

我正在尝试使用CATS函数(组合日期,月份和年份变量)创建日期变量。 当月份,日期或年份变量丢失时,我总是收到LOG错误。将字符M/D/Y字段转换为其中可能缺少一个或多个字符的日期

的LOG看起来像这样...

NOTE: Invalid argument to function INPUT at line 41 column 12. 
USUBJID=XYZ-01-002 event_seq=2 estd=31 estm=JAN esty=2013 eend=. eenm=FEB eeny=2013 
EVSTDT=31-JAN-2013 EVENDT=. _ERROR_=1 _N_=3 

这里是我的代码...

filename event URL "http://www.stat.wmich.edu/wang/680/data/event.csv"; 
RUN; 
Data events; 
Infile event delimiter=',' dsd firstobs=1; 
    Informat 
     USUBJID $10. event_seq 2. estd $UPCASE2. 
     estm $UPCASE3. esty $UPCASE4. eend $UPCASE2. 
     eenm $UPCASE3. eeny $UPCASE4.; 
    Input USUBJID event_seq estd estm esty eend eenm eeny; 
If estd = "UN" then estd = '.'; If eend = "UN" then eend = '.'; 
If eeny = "UNK" then eeny = '.'; If esty = "UNK" then esty = '.'; 
If esty = " " then esty = '.'; If eeny = " " then eeny = '.'; 
If eend = " " then eend = '.'; If estd = " " then estd = '.'; 
If estm = " " then estm = '.'; If eenm = " " then eenm = '.'; 
    Label 
    USUBJID = "Subject ID"   event_seq = "Event Sequence" 
    esty = "Estimated Start Year" estd = "Estimated Start Day" 
    estm = "Estimated Start Month" eend = "Estimated End Day" 
    eenm = "Estimated End Month" eeny = "Estimated End Year"; 
RUN; 
PROC SORT Data = events Out=ev_raw; 
BY USUBJID event_seq; 
RUN; 
DATA event_log; 
    Set ev_raw; 
     EVSTDT = input(cat(estd, estm, esty), date9.); 
     EVENDT = input(cat(eend, eenm, eeny), date9.); 
    Format 
     EVSTDT EVENDT date11.; 
RUN; 

谢谢您的帮助!

回答

3

当您输入缺少月份,日期或年份的日期时,您会得到什么结果?如果我告诉你一个日期是“2001年1月”,并且你被迫指向日历上的特定方块,那么你指的是哪个方块?

你需要有一些代码来处理这个。有时你可能会选择默认的day = 1,当day被省略时,默认的month = 1时,month被省略(强制day = 1,因为它不是特定的)。如果年份被排除在外,除了将整个日期设置为缺失之外,我不知道您会做什么,除非年份在您的特定用例中不是非常重要。其他人可能会将当天的默认值设置为15或将月份设置为6或7(因为这是可能值的平均值)。如果有任何组件丢失,其他人只需设置缺失的日期。

你选择做什么取决于你的数据和你的需求,但你必须给SAS一些工作;它不会为你做出决定。

+0

我想要做的是分配一个缺失值“”(空白),或者至少“。”。或“NA”。我知道SAS可能会遇到这个问题,但我不得不使用字符格式?例如... EVSTDT =输入(catx(“/”,estd,estm,esty),char11。); – k6adams 2014-09-29 19:19:25

+0

欢迎您将其存储为字符变量,但您目前没有这样做:将它输入到日期格式的数字中,该数字不能包含缺少组件值的这种概念。如果你想将它作为字符存储,只需使用'CATS'或'CATX'并且不要使用'input'(即'EVSTDT = catx('/',estd,estm,esty);'和更早的定义'EVSTDT'的长度为11)。 – Joe 2014-09-29 19:21:00

+0

at Joe ...我的编程知识大多局限于SAS,R和LaTeX。有没有可能指引我的地方,以便我知道如何妥善发布问题?谢谢你的帮助。 – k6adams 2014-09-29 19:25:30