1
votes

Problem Statement: I have a text file and I want to read it using SAS INFILE function. But SAS is not giving me the proper output.

Text File:
Akash    18 19 20
Tejas       20 16
Shashank 16 20
Meera    18    20

The Code that I have tried:

DATA Arr;
INFILE "/folders/myfolders/personal/SAS_Array .txt" missover;
INPUT Name$ SAS DS R;
RUN;

PROC PRINT DATA=arr;
RUN;

While the result i got is :

Table of Contents
Obs Name    SAS DS  R
1   Akash   18  19  20
2   Tejas   20  16  .
3   Shashank16  20  .
4   Meera   18  20  .

Which is improper. So what is wrong with the code? I need to read the file in SAS with the same sequence of marks as in text file. Please help.

Expected result:

Table of Contents
Obs Name    SAS DS  R
1   Akash   18  19  20
2   Tejas    .  20  16  
3   Shashank16  20  .
4   Meera   18   .  20  

Thanks in advance.

1
What is the delimiter in your text file? Are they tabs or spaces? It would also help if you showed what the output should be - user2877959
The text file is space delimited. And please check the code i have added the expected output. - Tejas Nikam
You edited the text file sample in your post. Now it really does not look like it is space-delimited but rather fixed width. Is that right? - user2877959
No its space delimited file only and sry i made a mistake while posting the text file. - Tejas Nikam
Then please post the sample text file correctly. Space-delimited means that values are separated by a space character. In your example, values are delimited by a variable amount of space characters. That is not space delimited. The file as posted here is fixed width. If you have a seemingly random number of spaces between values but you have no missing values in your text file, that is still workable with list input. But if values are separated by a variable amount of spaces and some values are missing, then there is no way to tell which one is missing. - user2877959

1 Answers

1
votes

If that text file is tab-delimited, you should specify the delimiter in the infile statement and use the dsd option to account for missing values:

DATA Arr;
INFILE "/folders/myfolders/personal/SAS_Array .txt" missover dlm='09'x dsd;
INPUT Name $ SAS DS R;
RUN;

PROC PRINT DATA=arr;
RUN;

EDIT: after editing, your sample text file now looks fixed-width rather than space-delimited. In that case you should be using column input:

DATA Arr;
INFILE "/folders/myfolders/personal/SAS_Array .txt" missover;
INPUT Name $1-9 SAS 10-12 DS 13-15 R 16-18;
RUN;

example with datalines:

DATA Arr;
INFILE datalines missover;
INPUT Name $1-9 SAS 10-12 DS 13-15 R 16-18;
datalines;
Akash    18 19 20
Tejas       20 16
Shashank 16 20
Meera    18    20
RUN;