Reading data from text files

Posts: 21

Reading data from text files


I have a text file (test4.txt) which has the following columns

Gender, Age, S1, S2, S3 and S4. Here S1-S4 are numeric variables.

The program or code below worked fine on SAS desktop learning edition. But when I ran it on the EG 4.3 it is not reading correctly.

The data on the last columns it not being read. Sometimes zero is read as zero and sometimes it is read as"blank/null"

How do I ensure that SAS reads the data correctly? Please advice.

The code I wrote is below:-

data lib2.test4;

infile "/home/sridhar78/sridharv/test4.txt";

input gender$ age s1 s2 s3 s4;


Data file "text4.txt"

Male 27 1 8 0 0

Female 29 3 14 5 10

Female 34 2 10 3 3

Male 35 2 112 4 8

Female 36 4 16 3 7

Male 21 1 5 0 0

Male 25 2 9 2 1

Female 21 1 4 2 6

The output I get is as below:-

Respected Advisor
Posts: 3,896

Re: Reading data from text files

Can you please attach your test file "test4.txt" so that we can have a look into it. I assume it has something to do with delimiters and/or end-of-line indicators.

Are you running your code under Windows or Unix?

You could try:

infile "/home/sridhar78/sridharv/test4.txt" TRUNCOVER;

Posts: 21

Re: Reading data from text files

Hi Patrick,

I'm unable to attach the txt file. Hence I've pasted the data above.

Test4 is a regular text file and the delimiter is space.

There are no inconsistencies such as missing data, wrong delimiter, etc.

To answer your question, I'm running the code in Windows.

I tried using TRUNCOVER, but still seeing the same output. Please advice.

thanks for your prompt response.

Respected Advisor
Posts: 3,896

Re: Reading data from text files

The forums here allow you to attach a file. What's holding you back?

The reason I'm after the original data source is that I suspect the issue is somewhere with whitespace characters (so a TAB instead of a BLANK or LF instead of CRLF as end-of-line indicators). This is something which can only be detected by looking into the original data source.

I kind-of suspect end-of-line indicators as interestingly the very last line of your data gets read as you want it. Does this file - by any chance - originate from a Unix environment?

Posts: 21

Re: Reading data from text files

I've attached the file by editing my question. I couldn't attach the file in my reply (that was the issue preventing me from attaching it).

Please check.

I'm not sure about the origin of the data file (whether Unix env. or not). I'm assuming it was created in windows.

Respected Advisor
Posts: 3,777

Re: Reading data from text files

You need to include the infile statement option TERMSTR=CRLF;

When you switched the program from desktop SAS to EG you changed the OS to UNIX, I suspect.

Ask a Question
Discussion stats
  • 5 replies
  • 3 in conversation