BookmarkSubscribeRSS Feed
GS2
Obsidian | Level 7 GS2
Obsidian | Level 7

Using SAS 9.4

 

I have a .txt file that is not delimited by anything and I need to import it into SAS. What is the best method for importing a file such as this so that the data is not shifted into incorrect columns? Also, to make it more difficult the file does not have column names so I would need to be able to name the column.Thank you

5 REPLIES 5
PaigeMiller
Diamond | Level 26

Not delimited? Not even by spaces?

 

Can you show us a small portion of this text file?

--
Paige Miller
GS2
Obsidian | Level 7 GS2
Obsidian | Level 7

I cannot show the text file as it has PHI throughout the file. What I have is a guide of what column name is associated with what positions on the row

PaigeMiller
Diamond | Level 26

While I have no idea what PHI is ... if you know the columns that the data is in, then you can read the text file using an INPUT statement in a DATA step. https://documentation.sas.com/?cdcId=pgmmvacdc&cdcVersion=9.4&docsetId=lestmtsref&docsetTarget=n13ej...

--
Paige Miller
Tom
Super User Tom
Super User

It is very easy in SAS to read a text file. Just use a data step.  Use the INFILE statement to tell SAS which file to read. Use the INPUT statement to tell it how to read the file.

 

For more help post a few lines of the file (mask any confidential information, but try to keep the relative position of the characters the same). Make sure to use the Insert Code icon (looks like {i} on the menu bar) to get a pop-up window where you can past the lines. That way the forum won't reflow the lines to try to make them look like paragraphs.

 

ballardw
Super User

@GS2 wrote:

Using SAS 9.4

 

I have a .txt file that is not delimited by anything and I need to import it into SAS. What is the best method for importing a file such as this so that the data is not shifted into incorrect columns? Also, to make it more difficult the file does not have column names so I would need to be able to name the column.Thank you


Did your data source give you any documentation about the structure of the file? If you have a document that says something like :

Last name is in columns 15 through 25 then you are golden. It is very easy to turn such a description into code.

 

If you do not nave such a document then the very first thing you need to do us get such documentation. Ask the file provider. Or if you downloaded from a website look for such documentation or contact the site for such.

 

Once you have the description you write a data step similar to this:

data example;
   input firstname $ 1-5  lastname $ 6-10 id $ 11-13 age 14-15 annualincome 16-20 ;
datalines;
MariaSmith1234567892
Johh Jones3334569878
;

You would not have datalines but an INFILE statement to indicate the file to read and needed characteristics.

 

You may need to specify informats for values like currency or dates.

 

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 438 views
  • 0 likes
  • 4 in conversation