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
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.
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
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?
number of lines is not an issue, the issue is I have to copy and paste all the lines into card?
@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?
@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
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;
The filename needs to be enclosed in quotes, just as you did in your initial post.
YAY! That worked perfectly!
Thanks,
Nazanin
@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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.