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

I have a CSV file that I am importing, and would prefer to have the import statement saved so that it can run regularly to import updates to the CSV file.  The file has a date variable stored as text in the format dd/mm/yyyy hh:mm (using 24 hr time).  I would prefer to see the imported date as DDMMYYYY HH:MM AM/PM.

 

I am importing on a data step using the infile statement.  Here is the gist of what I am running.

 

I get a blank column for date with the error: "Invalid argument to function INPUT".

 

Data Want;
infile "C:\My File.csv";
length code $12 date $19
input code $ date $ ;
date1 = input(date, Datetime19.);
drop date;
rename date1 = date;
run;
:  

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Informat DATETIME19. doesn't work because the value is not in the appropriate format. From the documentation:

 

The datetime values must be in the following form: ddmmmyy or ddmmmyyyy, followed by a blank or special character, followed by hh:mm:ss.ss (the time).

 

You don't have that. I leave it as a homework assignment for you to understand what parts of your text string dd/mm/yyyy hh:mm do not match.

 

This does work:

 

data have;
    datetimestring='19/02/2023 13:22';
run;
data want;
    set have;
    date1=input(datetimestring,anydtdtm.);
    format date1 datetime19.;
run;

 

 

Note: some people do not like the ANYDTDTM. informat because it is a guessing format, and it could guess wrong. If that's a concern, then this will work.

 

 

data want;
    set have;
    date1=input(scan(datetimestring,1,' '),ddmmyy10.);
    time1=input(scan(datetimestring,2,' '),time5.);
    date=dhms(date1,hour(time1),minute(time1),0);
    format date datetime19.;
run;
--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Informat DATETIME19. doesn't work because the value is not in the appropriate format. From the documentation:

 

The datetime values must be in the following form: ddmmmyy or ddmmmyyyy, followed by a blank or special character, followed by hh:mm:ss.ss (the time).

 

You don't have that. I leave it as a homework assignment for you to understand what parts of your text string dd/mm/yyyy hh:mm do not match.

 

This does work:

 

data have;
    datetimestring='19/02/2023 13:22';
run;
data want;
    set have;
    date1=input(datetimestring,anydtdtm.);
    format date1 datetime19.;
run;

 

 

Note: some people do not like the ANYDTDTM. informat because it is a guessing format, and it could guess wrong. If that's a concern, then this will work.

 

 

data want;
    set have;
    date1=input(scan(datetimestring,1,' '),ddmmyy10.);
    time1=input(scan(datetimestring,2,' '),time5.);
    date=dhms(date1,hour(time1),minute(time1),0);
    format date datetime19.;
run;
--
Paige Miller
RandoDando
Pyrite | Level 9
The first appears to have worked. I will compare to the original data to make sure.
yabwon
Onyx | Level 15

Maybe like this:

Data Want;
infile cards dlm="|";
input code : $ 12. date ANYDTTME.;
format date DATEAMPM23.;
cards;
abc|01/02/2003 04:05
;
run;
proc print data=Want;
run;

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1222 views
  • 1 like
  • 3 in conversation