BookmarkSubscribeRSS Feed
fengyuwuzu
Pyrite | Level 9
  data WORK.test    ;
    infile 'data.csv' delimiter = ',' MISSOVER DSD lrecl=32767 firstobs=2 ;
       informat ID $40. ;
       informat CreatedDT anydttme.  ;
       informat EndDT anydttme.  ;
       informat PeriodInDays best32. ;
       informat PeriodType $14. ;
       format ID $40. ;
       format CreatedDT time11. ;
       format EndDT time11. ;
       format PeriodInDays best12. ;
       format PeriodType $14. ;
    input
                ID $
                CreatedDT 
                EndDT 
                PeriodInDays
                PeriodType $
    ;
    run;

I am using the the above code to read in a csv file, with two time variables in the format like 12:02:27 AM. However, after reading in, these two variables have blank columns, and there is no error message in the log.

 

can anyone give some hints?

 

Thank you

 

4 REPLIES 4
Steelers_In_DC
Barite | Level 11

I think this will work for you:

 

data test;
informat time anydtdtm11.;
format time timeampm11.;
input time;
cards;
12:02:27 AM
;run;

FreelanceReinh
Jade | Level 19

@Steelers_In_DC: It's true that informat ANYDTDTM11. can read time values of the form HH:MM:SS AM/PM, but your data step does not prove this. You are using modified list input with the default delimiter ' ' (blank). That is, your input statement reads "12:02:27", but not the "AM" after the blank. If you change the "AM" in your data line to "PM" (or to an invalid string such as "XY"), you will see that TIME will continue to show as 12:02:27 AM. You could use the "&" modifier (input time &;) or change the delimiter (infile cards dlm=',';) or use formatted input (input time anydtdtm11.;) to read the value correctly.

 

@fengyuwuzu: Your code is syntactically correct. Informat ANYDTTME. is a good choice to read values like 12:02:27 AM. (There is no date part to be read, hence no need to apply a datetime informat such as ANYDTDTM., although the result would be the same. You are also right not using a length specification, e.g. "11" in the informat. This would be ignored anyway in the reading process, as you don't use formatted input [cf. documentation of the INFORMAT statement].)

 

So, the reason why you obtain only missing values for the CreatedDT and EndDT variables will be found in your data. Please note that the ANYDTTME. informat is very tolerant: Unlike, e.g., the TIME. informat, it does not create "Invalid data ..." messages even if your time value is the string "blahblah". Could you please attach a single line (or a few lines) of your csv file with data, so we can see why it is not being read correctly?

 

Alternatively, you could temporarily replace anydttme. with time. in your INFORMAT statements and then post the log messages for your data step. The TIME. informat is also suitable to read values like 12:02:27 AM, but it's more likely to produce an "Invalid data ..." message, if the data are in fact invalid.

Tom
Super User Tom
Super User

The example value you listed works fine for me.  Note that I find that it works better to explicitly define the variable types rather than asking SAS to guess how I want them defined by the INFORMAT or FORMAT that I have attached to them. Also attaching INFORMATS or FORMATS like $14. do not add any value and can cause trouble later if want to change the length of the variable. 

 

data WORK.test    ;
    infile cards dsd TRUNCOVER firstobs=2 ;
    length ID $40 CreatedDT EndDT PeriodInDays 8 PeriodType $14 ;
    informat CreatedDT anydttme.  ;
    informat EndDT anydttme.  ;
    format CreatedDT time11. ;
    format EndDT time11. ;
    input ID -- PeriodType ;
    put (_all_) (=);
cards;
ID,CreatedDT,EndDT,PeriodInDays,PeriodType
1,12:02:27 AM,12:02:27 PM,0,test
run;

Here are results for this data.

ID=1 CreatedDT=0:02:27 EndDT=12:02:27 PeriodInDays=0 PeriodType=test
NOTE: The data set WORK.TEST has 1 observations and 5 variables.
ballardw
Super User

Note that to display 12:02:27 AM you want to use a TIMEAMPM11. format, else you get the 0:02:27 time.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 819 views
  • 0 likes
  • 5 in conversation