DATA Step, Macro, Functions and more

Using Datalines

Reply
Regular Contributor
Posts: 199

Using Datalines

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.

 

Respected Advisor
Posts: 3,777

Re: Using Datalines

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;
Regular Contributor
Posts: 199

Re: Using Datalines

@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!

Respected Advisor
Posts: 3,124

Re: Using Datalines

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;
Respected Advisor
Posts: 4,641

Re: Using Datalines

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.

PG
Ask a Question
Discussion stats
  • 4 replies
  • 274 views
  • 3 likes
  • 4 in conversation