BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
htalbott
Fluorite | Level 6

I'm thoroughly confused about the following behavior in SAS.

 

I have a csv file import with a datetime variable. It is imported the way I expect using anydtdtm40. I was tracking down a bug and tried to create a datalines statement to replicate part of my dataset but the anydtdtm40. does not seem to be able to read in the "time" portion of datetime values that contain a space (which is how it is written in the CVS file). I cannot determine if I'm doing something wrong in my datalines statement, or if this is an expected behavior. If it is an expected behavior, is there a workaround?

 

I was reading up on the function of anydtdtm here: https://support.sas.com/documentation/cdl/en/leforinforref/64790/HTML/default/viewer.htm#p1hsn1ji141... which made it seem that a datetime value with a space should be okay.

 

CVS File Import:

data cvs_file ;
%let _EFIERR_ = 0; /* set the ERROR detection macro variable */
infile "&dir.have.csv" delimiter = ',' MISSOVER DSD lrecl=32767 firstobs=2 ;
informat datetimestuff anydtdtm40. ;
format datetimestuff datetime. ;
input
datetimestuff
;
if _ERROR_ then call symputx('_EFIERR_',1) ; /* set ERROR detection macro variable */
run ;

 

CVS File Import Output:

datetimestuff
07MAY22:02:10:00
07MAY22:02:10:00
01JAN12:14:30:08
01JAN12:14:30:08

 

Datalines Statment (written the same way as in the CVS file):

data datalines_input ;
attrib datetimestuff informat=ANYDTDTM40. format=datetime. ;
input datetimestuff ;
datalines;
2022-05-07 02:10:00
2022-05-07:02:10:00
01JAN12 14:30:08
01JAN12:14:30:08
;
run;

 

Datalines Statement Output:

datetimestuff
07MAY22:00:00:00
07MAY22:02:10:00
01JAN12:00:00:00
01JAN12:14:30:08

 

Software

  • SAS 9.4 TS Level 1M7
  • X64_10PRO platform

Operating System Information

  • Windows Version 1.0.17763
1 ACCEPTED SOLUTION

Accepted Solutions
htalbott
Fluorite | Level 6

Walked away and then proceeded to figure out how to make this work.

data datalines_input ;
	infile datalines delimiter=',' ; 
	attrib datetimestuff informat=ANYDTDTM40. format=datetime. ;
	input datetimestuff ;
    datalines ;
2022-05-07 02:10:00
2022-05-07:02:10:00
01JAN12 14:30:08
01JAN12:14:30:08
;
run;

 

Added this statement " infile datalines delimiter=',' ; " 

View solution in original post

3 REPLIES 3
htalbott
Fluorite | Level 6

Walked away and then proceeded to figure out how to make this work.

data datalines_input ;
	infile datalines delimiter=',' ; 
	attrib datetimestuff informat=ANYDTDTM40. format=datetime. ;
	input datetimestuff ;
    datalines ;
2022-05-07 02:10:00
2022-05-07:02:10:00
01JAN12 14:30:08
01JAN12:14:30:08
;
run;

 

Added this statement " infile datalines delimiter=',' ; " 

Tom
Super User Tom
Super User

If there is only a single space you could also just add the & modifier to the INPUT statement for that variable.

That says that it requires two delimiters to indicate the end of a value.

 

If you are reading a single value from the line you could just use formatted input instead of LIST MODE input.  That will read the number of characters specified by the informat, whether or not there are any delimiter characters there.

 

Example:

data datalines_input ;
  input @1 dt1 & :anydtdtm. 
        @1 dt2 anydtdtm40.
  ;
  format dt1 dt2 datetime19.;
datalines ;
2022-05-07 02:10:00
2022-05-07:02:10:00
01JAN12 14:30:08
01JAN12:14:30:08
;
Obs                    dt1                    dt2

 1      07MAY2022:02:10:00     07MAY2022:02:10:00
 2      07MAY2022:02:10:00     07MAY2022:02:10:00
 3      01JAN2012:14:30:08     01JAN2012:14:30:08
 4      01JAN2012:14:30:08     01JAN2012:14:30:08
htalbott
Fluorite | Level 6
Thanks Tom for the additional ways to make this work!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 564 views
  • 1 like
  • 2 in conversation