Reading one Record scattered over multiple lines

In certain scenarios; the external file may contain a single record scattered over multiple lines.

For example in the file below, every single record we want to capture is spread over two lines of the external file.

The first line contains the values for the acc_no, acc_open_date, cust_type_code while the second line contains the cust_Fname, cust_Lname, cust_age and cust_gender.

In short the two lines of the external file contains the data for single customer; which we want to capture as single record into the SAS system.

External File: accDetails.txt

000573691345 10JUL2010 C

Rogger Jones 32 M

000456234567 23JUN2011 O

Nicole Davis 23 F

101345456734 21SEP1999 O

Rachel Brown 27 F

101456234561 30OCT2011 C

Michael Daniel 29 M

Lets see how we can achieve this with following SAS code.

SAS Code

/* Code 1 - Using Multiple Input Statements */

data temp;

infile "D:\accDetails.txt";

input acc_no acc_open_dt date9. cust_type_code $2.;

input cust_Fname $ cust_Lname $ cust_age cust_gender $1.;

run;

proc print data=temp;

title "Ouput of Code 1 - applied format for acc open date";

format acc_open_dt date9.;

run;

/* Code 2 - Using "/" character to move input pointer to next line of external file */

data temp;

infile "D:\accDetails.txt";

input acc_no acc_open_dt date9. cust_type_code $2. /

cust_Fname $ cust_Lname $ cust_age cust_gender $1.;

run;

proc print data=temp;

title "Ouput of Code 2 - applied format for acc open date";

format acc_open_dt date9.;

run;

/* Code 3 - Using absolute potiner control "#" */

data temp;

infile "D:\accDetails.txt";

input #1 acc_no acc_open_dt date9. cust_type_code $2.

#2 cust_Fname $ cust_Lname $ cust_age cust_gender $1.;

run;

proc print data=temp;

title "Ouput of Code 3 - applied format for acc open date";

format acc_open_dt date9.;

run;

In SAS the code above we have implemented 3 methods to achieve the same output.

Method 1: Using multiple INPUT statements each one for every single line of external file but corresponding to single logical record.

Method 2: Just using '/' character which moves the input line pointer to next line of the external file.

Method 3: Using absolute pointer control which moves input line pointer to specific line provided with '#' character.

And following is the output achieved using each of the methods described above.

Output:

Ouput of Code 1 - applied format for acc open date

cust_

acc_open_ type_ cust_ cust_ cust_ cust_

Obs acc_no dt code Fname Lname age gender

1 573691345 10JUL2010 C Rogger Jones 32 M

2 456234567 23JUN2011 O Nicole Davis 23 F

3 101345456734 21SEP1999 O Rachel Brown 27 F

4 101456234561 30OCT2011 C Michael Daniel 29 M

Ouput of Code 2 - applied format for acc open date

cust_

acc_open_ type_ cust_ cust_ cust_ cust_

Obs acc_no dt code Fname Lname age gender

1 573691345 10JUL2010 C Rogger Jones 32 M

2 456234567 23JUN2011 O Nicole Davis 23 F

3 101345456734 21SEP1999 O Rachel Brown 27 F

4 101456234561 30OCT2011 C Michael Daniel 29 M

Ouput of Code 3 - applied format for acc open date

cust_

acc_open_ type_ cust_ cust_ cust_ cust_

Obs acc_no dt code Fname Lname age gender

1 573691345 10JUL2010 C Rogger Jones 32 M

2 456234567 23JUN2011 O Nicole Davis 23 F

3 101345456734 21SEP1999 O Rachel Brown 27 F

4 101456234561 30OCT2011 C Michael Daniel 29 M