BookmarkSubscribeRSS Feed
NazaninSAS
Quartz | Level 8

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

10 REPLIES 10
Kurt_Bremser
Super User

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.

NazaninSAS
Quartz | Level 8

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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?

NazaninSAS
Quartz | Level 8

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

andreas_lds
Jade | Level 19

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

Kurt_Bremser
Super User

@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

NazaninSAS
Quartz | Level 8

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;

 

NazaninSAS
Quartz | Level 8

YAY! That worked perfectly!

 

Thanks,

 

Nazanin

ballardw
Super User

@Kurt_Bremser 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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 10 replies
  • 8148 views
  • 0 likes
  • 5 in conversation