Learning SAS? Welcome to the exclusive online community for all SAS learners.

Can someone explain this what is happening here?

Reply
Occasional Contributor
Posts: 5

Can someone explain this what is happening here?

[ Edited ]

Hi,

 

I am a beginner and was running this code, but didn't able to figure out why the results are coming like this.

 

Code: (case 1)

DATA tempDS;
	INFILE DATALINES;
	INPUT
		date1	MMDDYY10.
		date2	MMDDYY10.
		date3	DATE9.
	;
	FORMAT
		date1	MMDDYY10.
		date2	MMDDYY10.
		date3	MMDDYY10.
	;
	

DATALINES;
01/03/1950 01/03/1960 03Jan1970
05/15/2000   05/15/2002   15May2003
10/10/1998 11/12/2000   25Dec2005
;
RUN; TITLE "BLAH BLAH BLAH"; PROC PRINT DATA=tempds; RUN;

(The columns do not line up in DATALINES).

 

The result is:

                   BLAH BLAH BLAH 
Obs         date1               date2         date3
1          01/03/1950             .             .
2          05/15/2000        05/15/2002         .
3          10/10/1998             .             .

There are too many questions here:

1) Why the date2, date3 in rows 1 and 3 are missing?

2) How date2 in row 2 is being read correctly here?

 

And if I run this code: (case 2)

DATA tempDS;
	INFILE DATALINES;
	INPUT
		date1	: MMDDYY10.
		date2	: MMDDYY10.
		date3	: DATE9.
	;
	FORMAT
		date1	MMDDYY10.
		date2	MMDDYY10.
		date3	MMDDYY10.
	;
	

DATALINES;
01/03/1950 01/03/1960 03Jan1970
05/15/2000   05/15/2002   15May2003
10/10/1998 11/12/2000   25Dec2005
;
RUN;

TITLE "BLAH BLAH BLAH";
PROC PRINT DATA=tempds;
RUN;

The results are coming as expected:

                    BLAH BLAH BLAH 
Obs         date1               date2         date3
1          01/03/1950        01/03/1960     01/03/1970
2          05/15/2000        05/15/2002     05/15/2003
3          10/10/1998        11/12/2000     12/25/2005

Please can someone explain how the data is being read (in both cases) here. How colon ' : ' modifier works as I know that:

"The colon ' : ' modifier allows you to read nonstandard data values and character values that are longer than
eight characters, but which have no embedded blanks."

Using:

SAS release: 9.04

SAS studio - UNIVERSITY EDITION 2.7 9.4 M5.

 

Thanks & Regards!

Smiley Happy

Super User
Posts: 6,931

Re: Can someone explain this what is happening here?

Posted in reply to nahideraha

Consider the first line of data.  Note that the proper value for DATE2 begins in column 12 on that line.

 

Without using the colon, the first INPUT statement tells SAS to look for DATE2 starting at its current location on the line of data.  Since the program just read DATE1 from columns 1 through 10, the current location is column 11.  So SAS is trying to read DATE2 from columns 11 through 20, when it should be using columns 12 through 21.

 

The colon tells SAS to look through the line of data until it finds something (anything that isn't blank).  So SAS finds nothing in column 11 and moves on to column 12.  There, it finds a nonblank, so it applies the INPUT instructions (MMDDYY10.) starting at column 12.

 

 

SAS Super FREQ
Posts: 9,426

Re: Can someone explain this what is happening here?

[ Edited ]
Posted in reply to Astounding

And, the OTHER thing that the colon modifier does is that it informs the INPUT statement that the numeric value (in this case) the DATE variables are "non-standard" and must be read with an INFORMAT. When you use an INFORMAT after the colon modifier, you do NOT need to put a width. This works too:

DATA tempDS;
	INFILE DATALINES;
	INPUT
		date1 :	MMDDYY.
		date2 : MMDDYY.
		date3 :	DATE.	;
	FORMAT
		date1	MMDDYY10.
		date2	MMDDYY10.
		date3	MMDDYY10.	;
RETURN;
DATALINES;
01/03/1950 01/03/1960 03Jan1970
05/15/2000   05/15/2002   15May2003
10/10/1998 11/12/2000   25Dec2005
;
RUN;
  
proc print data=tempDS;
  title 'modified program';
run;



Cynthia

Ask a Question
Discussion stats
  • 2 replies
  • 200 views
  • 1 like
  • 3 in conversation