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

Hi all,

 

I am currently trying to import a CSV file and keep getting the below Note;

NOTE: Invalid data for STARTDATE in line 2 444-462.

I know that this is being caused due to the data which is trying to be read in is 22/07/2018 09:56:20 in the UK format of dd/mm/yyyy hh:mm:ss but I cannot seem to find the right informat to properly read the data.

 

I have tried DATE, DATETIME, ANDTTME, even E8601DX (all with a width of 16) but nothing seems to work.

 

I am using a data step and defining lengths, formats, informats etc., and I am also using SAS EG 7.15 (7.100.5.5850) (64-bit) to access  a LINUX server running 9.4.

 

Any help would be greatly appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
SuryaKiran
Meteorite | Level 14

You can find the answer by yourself, In SAS EG use the Import Wizard (File>Import Data in SAS Eg) to read the data from CSV. Import wizard will anatomically identify the informat and formats in most of the cases. 

 

It will create the code and you can copy the code and make some changes as per your requirement. 

 

For this scenario it gave me ANYDTDTM19. informat.

 

Thanks,
Suryakiran

View solution in original post

10 REPLIES 10
Jagadishkatam
Amethyst | Level 16

Please use the below code in your datastep for the date variable.

 

 

data want;
INFILE '/home/test.csv' dsd;
 
 input datevar:ANYDTDTM.;
 
 format datevar DATETIME19.;
put datevar= DATETIME19.;
 
 run;

 

Thanks,
Jag
Kurt_Bremser
Super User

Personally, I would prefer to force strict formats. I don't trust the guessing involved by using the ANYDTDTM informat.

data want;
input string $19.;
datetime_value = dhms(input(substr(string,1,10),ddmmyy10.),0,0,input(substr(string,12),time8.));
format datetime_value e8601dt19.;
cards;
22/07/2018 09:56:20
;
run;
mwstat
Calcite | Level 5

Hi,

I tried to use this suggestion to read in data that came to me like this

6/28/2021 22:51
6/28/2021 23:41
6/29/2021 2:18
6/29/2021 5:35
6/29/2021 2:13

I modified the string to be 1,9 and the date to be mmddyy but no luck

 

Any suggestions?

TIA

 

Kurt_Bremser
Super User

You need to adapt the statement to the variable length of the date, the MDY order, and the shorter time:

datetime_value = dhms(input(scan(string,1),mmddyy10.),0,0,input(scan(string,2),time5.));
mwstat
Calcite | Level 5

Thanks for the suggestions. I wanted to come back and say that I have also had success using

 

 

data want1;
set attempt_nov;
time_created1= timepart(DTAttempt);
date_created1 = datepart(DTAttempt);
week1=week(date_created1);
format date_created1 mmddyy10. time_created1 time.;
run;

Tom
Super User Tom
Super User

@mwstat wrote:

Thanks for the suggestions. I wanted to come back and say that I have also had success using

 

 

data want1;
set attempt_nov;
time_created1= timepart(DTAttempt);
date_created1 = datepart(DTAttempt);
week1=week(date_created1);
format date_created1 mmddyy10. time_created1 time.;
run;


That will only work if you already have a DATETIME variable, not the character string that the original question is asking about.

SuryaKiran
Meteorite | Level 14

You can find the answer by yourself, In SAS EG use the Import Wizard (File>Import Data in SAS Eg) to read the data from CSV. Import wizard will anatomically identify the informat and formats in most of the cases. 

 

It will create the code and you can copy the code and make some changes as per your requirement. 

 

For this scenario it gave me ANYDTDTM19. informat.

 

Thanks,
Suryakiran
trt
Fluorite | Level 6 trt
Fluorite | Level 6

I have to say this is the worst format to take for import.

 

 ANYDTDTM19. informat is choosing format for each single new line, and makes serious mistakes when month and day can be misunderstood. For example 12JUN2018 (12.06.2018) will by rule always misinterpreted as 06DEC2018.

 

That is why it is very important to set options datestyle=dmy;

to make it sure that day is read always first.

SanderB
Fluorite | Level 6
This answer is actually wrong because ANYHDTDTM will assume MM-DD-YYYY first over DD-MM-YYYY. If you give it 12-01-2023 it will read it as the 1st of December, but it should the 12th of January.
Kurt_Bremser
Super User

@SanderB wrote:
This answer is actually wrong because ANYHDTDTM will assume MM-DD-YYYY first over DD-MM-YYYY. If you give it 12-01-2023 it will read it as the 1st of December, but it should the 12th of January.

This is not (entirely) correct. The ANY... informats will prefer DMY or MDY depending on the DATESTYLE system option (which has a default setting of LOCALE, which means the current locale setting also influences the date order).

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
  • 10 replies
  • 22500 views
  • 9 likes
  • 8 in conversation