SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sathya66
Barite | Level 11

I am trying to read a text file into SAS by using infile statement but losing columns.

 

each blank/space is a column.

see the attached test file

data a;
infile "/sas/test.txt" dsd missover lrecl = 32767;
input  a$1-10 b$14-18 c$: @44d$1. e$: f$ g$ h$ i$ j$ k$;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@sathya66 wrote:

All are Chars.
first row of the the file is a datestamp . records with blank space is a column.
yes 20 columns and we know the column names.
ex:1N5596194 0716 0020000M M M M M M M M M M M M M M M M M M
160944392 0716 0046001C C C C C C I I I I I I I I I I I I

 

I can read the data into SAS by using import data wizard from SAS EG but not with the code (If I copy the code from "last submitted code or from the log" of import data wizard, it doesn't read the file correctly 

 


Those a NOT variable names.  That is just some example lines from the file.

Does the Enterprise Guide wizard actually allow you to generate code to read a file that is not a delimited file? I thought the import wizard only supported reading delimited files?

 

But if you already know the names of the variables (and the types and lengths they should have) then there is no need to ask SAS to GUESS how to read the file.  Just write a program.  

 

Something like:

data want;
   infile 'myfile.txt' truncover ;
   if _n_=1 then input datestamp :yymmdd. ;
   format datestamp yymmdd10.;
   retain datestamp;
   input var1 $1-10 var2 11-12 .... @42 (m1-m19) ($2.);
run;

To get help creating a data step that will actually read the data you have share the documentation that describes the file format.

View solution in original post

6 REPLIES 6
Tom
Super User Tom
Super User

That file does appear to be using space delimiters. Instead it appears to be fixed column.  There appear to be some initial variables then a long list of variables that are each using two characters.  Are they supposed be read as character? Or are they numeric variables with special missing values of I, C and M?

 

Do you have documentation on how many variables there are and what their meaning is?  Does it indicate either what columns they appear in or the number of characters they take on the line?

 

To read a series of variables with a common informat you can use syntax like this as part of your INPUT statement.  Inside the first ( ) is the list of variables to be read. Inside the second ( ) is the list of informats to use.  If the list of variables is longer than the list of informats then the list of informats is reused from the beginning.

 

  @42 (v1-v18) ($2.) 

 

 

 

sathya66
Barite | Level 11

All are Chars.
first row of the the file is a datestamp . records with blank space is a column.
yes 20 columns and we know the column names.
ex:1N5596194 0716 0020000M M M M M M M M M M M M M M M M M M
160944392 0716 0046001C C C C C C I I I I I I I I I I I I

 

I can read the data into SAS by using import data wizard from SAS EG but not with the code (If I copy the code from "last submitted code or from the log" of import data wizard, it doesn't read the file correctly 

 

Tom
Super User Tom
Super User

@sathya66 wrote:

All are Chars.
first row of the the file is a datestamp . records with blank space is a column.
yes 20 columns and we know the column names.
ex:1N5596194 0716 0020000M M M M M M M M M M M M M M M M M M
160944392 0716 0046001C C C C C C I I I I I I I I I I I I

 

I can read the data into SAS by using import data wizard from SAS EG but not with the code (If I copy the code from "last submitted code or from the log" of import data wizard, it doesn't read the file correctly 

 


Those a NOT variable names.  That is just some example lines from the file.

Does the Enterprise Guide wizard actually allow you to generate code to read a file that is not a delimited file? I thought the import wizard only supported reading delimited files?

 

But if you already know the names of the variables (and the types and lengths they should have) then there is no need to ask SAS to GUESS how to read the file.  Just write a program.  

 

Something like:

data want;
   infile 'myfile.txt' truncover ;
   if _n_=1 then input datestamp :yymmdd. ;
   format datestamp yymmdd10.;
   retain datestamp;
   input var1 $1-10 var2 11-12 .... @42 (m1-m19) ($2.);
run;

To get help creating a data step that will actually read the data you have share the documentation that describes the file format.

sathya66
Barite | Level 11

 

We know the cloumn names but not the lenghts.I will chase for the lenghts.

Yes,It will give you the code in the log or last submitted code. see below

sathya66_0-1637072963791.png

 

 

sathya66_1-1637072963798.png

 

Tom
Super User Tom
Super User

No that is showing what Enterprise Guide "guessed" were the variables in the file.  You need to share the documentation you got from the person/place where you got the original text file with the data.

 

Your file is NOT a delimited file so do not try to read as such.

 

From your photograph it looks like EG will let you use that wizard to create code to read a fixed column file.  If you are uncomfortable writing code you could try using that. Click the "Fixed columns" button in the form in your first picture and see what that wizard wants you to do.

 

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 16244 views
  • 1 like
  • 3 in conversation