I'm trying to better understand using the DATALINES statement so that I can use it to quickly test functions, etc. while working on a SAS project. I want to be able to include LENGTH, INFORMATS and FORMATS.
The following code produces a few errors:
DATA test; INPUT date1 : mmddyy8. date2 : mmddyy10. date3 : mdyampm25.2; DATALINES; 6/6/11 4/4/2012 03/26/12 12:58 AM 7/6/11 3/4/2012 10/26/12 10:58 AM ; FORMAT date1 date2 date3 datetime9.; RUN;
I am getting an "invalid data" warning at each row for date1: "NOTE: Invalid data for date1 in line 62 1-7."
Also, the FORMAT statement is producing an error.
Is there a better approach to the one I'm using here?
Thanks for your assistance.
You have a TAB in your data lines you can use INFILE statement option to remove it. Also your FORMAT statement cannot come after datalines statement. Finally and I think this is it you need & INFORMAT modified for date3 to read embedded blanks.
DATA test;
infile datalines expandtabs;
INPUT
date1 : mmddyy10.
date2 : mmddyy10.
date3 & mdyampm25.2;
FORMAT date1 date2 date3 datetime9.;
DATALINES;
6/6/11 4/4/2012 03/26/12 12:58 AM
7/6/11 3/4/2012 10/26/12 10:58 AM
;;;;
RUN;
proc print;
run;
@data_null__ Thanks for your help here.
The embedded blanks are the spaces between the date and time? And between the last digit of the time and AM/PM?
I can I determine, going forward, whether an informat accounts for those blanks, or whether I need the "&"? What exactly does the "&" do?
Thanks!
Similar to what @data_null__'s suggestion, only add different format to help keeping time part information:
DATA test;
infile datalines TRUNCOVER EXPANDTABS ;
INPUT
date1 : mmddyy10.
date2 : mmddyy10.
date3 : & mdyampm25.2;
FORMAT date1 date2 DATE9.
date3 dateAMPM25.2;
DATALINES;
6/6/11 4/4/2012 03/26/12 12:58 AM
7/6/11 3/4/2012 10/26/12 10:58 AM
;
RUN;
One more thing, if you want to save typing a line of code. You never need a RUN statement after a datalines (or cards) block. Execution starts as soon as the block ends.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.