BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
JohanK
Calcite | Level 5

Dear reader, I am asking for help...

 

I am trying to import a TXT file with just text, but also ; : TAB etc..

I want to get the full file imported- just one variable, the file has 1861 rows but it only imports 20 of them

 

This is the code for importing: 

PROC IMPORT DATAFILE="C:\Output\log.txt"
OUT=LOGFILE
DBMS=TAB REPLACE;
GETNAMES=NO;
RUN;

 

My LOG: 

NOTE: WORK.LOGFILE data set was successfully created.
NOTE: The data set WORK.LOGFILE has 20 observations and 1 variables.
NOTE: PROCEDURE IMPORT used (Total process time):
real time 0.08 seconds
cpu time 0.04 seconds

 

Why are 20 lines imported and what can I best do? 

I prefer PROC IMPORT...

Many thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

But I prefer to use data step ,that is simple and easy,if you only want ONE variable in dataset.

 

data x;
infile  "C:\temp\a\log.txt" termstr=crlf length=len ignoredoseof;
input x $varying1000. len;
run;

Ksharp_0-1697630924265.png

 

View solution in original post

12 REPLIES 12
PaigeMiller
Diamond | Level 26

PROC IMPORT is a guessing procedure. It guesses how the fields are separated, and in this case it guessed wrong.

 

You can read the data in a DATA step, where you use the INFILE statement with the DLM= option. The DLM= option allows you to specify more than one different delimiters that the file uses. https://documentation.sas.com/doc/en/pgmmvacdc/9.4/lestmtsref/n1rill4udj0tfun1fvce3j401plo.htm#n14rj...

 

In fact, PROC IMPORT will create (incorrect) code for you, look in the log. You should be able to modify the code so that the DATA step reads the data properly.

--
Paige Miller
JohanK
Calcite | Level 5
Dear Paigemiller, many thanks for your reply. I have a solution via another user.
ballardw
Super User

Why did you bother to attach that log? There is NO code that imports any txt file. Everything I see that calls proc import is for XLSX.

 

Unless asked only include log for pieces of code that you have a question about. Since there is nothing in that log with

PROC IMPORT DATAFILE="C:\Output\log.txt" it is useless. And the snippet that you show is not the complete log because if you use Proc Import with a Tab delimited file then there will be generated data step code.

 

Causes for fewer observations than you expect could be the file source uses a different end of line marker.

Which in a data step you use the INFILE statement to set the TERMSTR= option to see if that is the case. TERMSTR=CRLF for Windows source files, TERMSTR=LF for Unix Linux and derivitives and TERMSTR=CR for some other sources.

 

 

JohanK
Calcite | Level 5
Dear ballardw, I have a solution via another user. Many thanks though
Ksharp
Super User

You have EOF(end of file) character at 21th row, therefore sas stop reading file when running into it .

Ksharp_0-1697630442411.png

 

 

Add option IGNOREDOSEOF to ignore EOF character to read this fill all.

filename x "C:\temp\a\log.txt" termstr=crlf ignoredoseof;

PROC IMPORT DATAFILE=x
OUT=LOGFILE
DBMS=TAB REPLACE;
GETNAMES=NO;
guessingrows=max;
RUN;

Ksharp_1-1697630521202.png

 

Ksharp
Super User

But I prefer to use data step ,that is simple and easy,if you only want ONE variable in dataset.

 

data x;
infile  "C:\temp\a\log.txt" termstr=crlf length=len ignoredoseof;
input x $varying1000. len;
run;

Ksharp_0-1697630924265.png

 

JohanK
Calcite | Level 5

Thank you so much Amir, it works ! 

Greetings, Johan

JohanK
Calcite | Level 5

sorry, not Amir but Mr. Sharp... 

Amir
PROC Star

Hi,

 

I'm not sure that I fully understand your requirement, there is mention of Tab characters in the data and that you only want one variable for the whole file, but Tab is in your proc import code too; maybe I'm missing something.

 

I see that you've said you prefer proc import, but as you are having problems with it, you can see if a data step alternative works for, e.g., as per the below untested code (you can change the log_record length if necessary):

 

data logfile;
  infile "C:\Output\log.txt" truncover;
  input log_record $char256.;
run;

 

 

Thanks & kind regards,

Amir.

Tom
Super User Tom
Super User

You don't need to IMPORT text files since SAS can just READ a text file.

 

The only reason to use PROC IMPORT to read a delimited file is if you want it to try to GUESS how to read it.

 

And if you don't have a delimited file there is nothing that it can GUESS for you.

 

So just read the file yourself with a DATA step.

JohanK
Calcite | Level 5
I have a solution via another user Tom, thanks for your reply!
Kurt_Bremser
Super User

Read this log file with a DATA step and use the proper line termination string:

data log;
infile "log.txt" termstr=LF truncover;,
input line $256.;
run

LF is for text files originating from a UNIX environment. For Windows, use CRLF.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 12 replies
  • 2675 views
  • 3 likes
  • 7 in conversation