DATA Step, Macro, Functions and more

How to import a non delimited text file

Reply
Contributor
Posts: 24

How to import a non delimited text file

Hi,

 

I would like to import a non delimited text file to SAS. My data looks like this :

99999999200303319999123101010120030609                    41
99999999201511029999123101040120150331                    56
99999999201701039999123102040220170113                     
99999999200708219999123101040120071010                  80 
99999999201803079999123102060220180307                     
99999999200303319999123102010220030609                     
999999992013042999991231010402201305072323                56

 

My data set should have 18 fields with following lengths:

field1:8

field2:8

field3:8

field4:2

field5:2

field6:2

field7:8

Field8-18 : 2

 

some fields can have no values.

 

I have already done that, but the problem is that the program does not run line by line, so I get mixed up values in my dataset.

data SI_July;

infile "H:\EE NAZANIN 2012\SAS\My Training.SIJuly.txt";

dlm='' LRECL=32760 ;

 

length

PRI $8 From $8 To $8 Field4 $1 Field5 $1 Field6 $1 Field7 $8 Dis1 $2 Dis2 $2

Dis3 $2 Dis4 $2 Dis5 $2 Dis6 $2 Dis7 $2 Dis8 $2 Dis9 $2 AborCode $2 VisminCode $2;

 

input

PRI From To Field4 Field5 Field6 Field7 Dis1 Dis2 Dis3 Dis4 Dis5 Dis6 Dis7 Dis8 Dis9 AborCode VisminCode;

run;

 

 

Thanks a lot,

 

Nazanin

Super User
Posts: 10,526

Re: How to import a non delimited text file

Posted in reply to NazaninSAS

With fixed columns and no delimiter, you use formatted input:

data want;
infile cards truncover;
input
  f1 $8.
  f2 $8.
  f3 $8.
  f4 $2.
  f5 $2.
  f6 $2.
  f7 $8.
  (f8-f18) ($2.)
;
cards;
99999999200303319999123101010120030609                    41
99999999201511029999123101040120150331                    56
99999999201701039999123102040220170113                     
99999999200708219999123101040120071010                  80 
99999999201803079999123102060220180307                     
99999999200303319999123102010220030609                     
999999992013042999991231010402201305072323                56
;
run;

Note the truncover option, which prevents skipping into the next row.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 24

Re: How to import a non delimited text file

Posted in reply to KurtBremser

Thanks Kurt, my problem is my input is much larger than that. I have about 90,000 lines.

in this case, what do you suggest?

 

Best,

 

Nazanin

Super User
Super User
Posts: 9,794

Re: How to import a non delimited text file

Posted in reply to NazaninSAS

Why would number of lines be an issue?  Your import states the structure to read the data in, then is reads each line outputting to the dataset.  Number of lines is irrelevant to anything but the number of observations output?

Contributor
Posts: 24

Re: How to import a non delimited text file

number of lines is not an issue, the issue is I have to copy and paste all the lines into card?

Valued Guide
Posts: 621

Re: How to import a non delimited text file

Posted in reply to NazaninSAS

@NazaninSAS wrote:

Thanks Kurt, my problem is my input is much larger than that. I have about 90,000 lines.

in this case, what do you suggest?

 

Best,

 

Nazanin


The number of lines does not matter. Or do you have 90k columns?

Super User
Posts: 10,526

Re: How to import a non delimited text file

Posted in reply to NazaninSAS

@NazaninSAS wrote:

Thanks Kurt, my problem is my input is much larger than that. I have about 90,000 lines.

in this case, what do you suggest?

 

Best,

 

Nazanin


Irrelevant. You drop the cards; block from my example data step and replace cards in the infile statement with your filename. That's all.

See the documentation for cards/datalines

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 24

Re: How to import a non delimited text file

Posted in reply to KurtBremser

just to be clear, this is what you suggest?

 

data want;

infile H:\EE NAZANIN 2012\SAS\My Training\SIJuly3.txt truncover;

 

input

f1 $8.

f2 $8.

f3 $8.

f4 $2.

f5 $2.

f6 $2.

f7 $8.

(f8-f18) ($2.)

 

;

run;

 

Super User
Posts: 10,526

Re: How to import a non delimited text file

Posted in reply to NazaninSAS

The filename needs to be enclosed in quotes, just as you did in your initial post.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 24

Re: How to import a non delimited text file

Posted in reply to KurtBremser

YAY! That worked perfectly!

 

Thanks,

 

Nazanin

Super User
Posts: 13,868

Re: How to import a non delimited text file

Posted in reply to KurtBremser

@KurtBremser wrote:

With fixed columns and no delimiter, you use formatted input:

data want;
infile cards truncover;
input
  f1 $8.
  f2 $8.
  f3 $8.
  f4 $2.
  f5 $2.
  f6 $2.
  f7 $8.
  (f8-f18) ($2.)
;
cards;
99999999200303319999123101010120030609                    41
99999999201511029999123101040120150331                    56
99999999201701039999123102040220170113                     
99999999200708219999123101040120071010                  80 
99999999201803079999123102060220180307                     
99999999200303319999123102010220030609                     
999999992013042999991231010402201305072323                56
;
run;

Note the truncover option, which prevents skipping into the next row.


Are all of these "fields" actually character? if columns 9 through 16, Field2, are actually a date you might want to use a date informat such as yymmdd8. and assign a proper date format liked yymmdd8. or Date9. for display (and I think the same thing for Field7) . If you need to do anything with dates having an actual date valued numeric will be much easier. Any actual numeric values you expect to do calculations with should be read as such with a numeric informat, such as F2. or  at this time as well. Otherwise you will be asking how to change your character values to numeric before doing any additional work.

And descriptive variable names and/or labels might help as well.

Ask a Question
Discussion stats
  • 10 replies
  • 130 views
  • 0 likes
  • 5 in conversation