Help using Base SAS procedures

Importing Text File

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

Importing Text File

Hi,

I have a .txt file that looks like this:

Advanced Breast Center|Carr.   14 KM 72.2|Plaza Milliangie||Cayey|PR|00736|787738-8077|787735-0718

Advanced Diagnostic Imaging Center, LLC|1728 Village Park Dr.|||Orangeburg|SC|29118|803536-1106|803535-6033

Advanced Diagnostic Radiology, LLC|952 Seton Dr.|||Cumberland|MD|21502|301777-3522|301777-1902

Advanced Imaging|35 Batavia City Centre Main St|||Batavia|NY|14020|585345-9729|585345-9823

So it looks like it is "|" delimited, and I also have this information- Fields Name and Field Length:

Facility Name75
Address 150
Address 250
Address 350
City50
State2
Zip Code15
Phone50
Fax50

How can I import this into SAS?


Accepted Solutions
Solution
‎11-24-2014 03:54 PM
Super User
Super User
Posts: 6,502

Re: Importing Text File

Copy your simple variable definitions and paste them into your program editor and turn them into a LENGTH statement.  The basically will involve adding the keyword LENGTH before, editing the names to be valid SAS variable names (getting rid of the embedded spaces), add a $ before the length for character strings (for numeric variables don't add the $ but do convert the "length" to 8 since SAS stores all numbers as 8 byte floating point numbers) and add a semicolon on the end.  Then just use it in a simple DATA step.

data want ;

  length

FacilityName $75

Address1 $50

Address2 $50

Address3 $50

City $50

State $2

ZipCode $15

Phone $50

Fax $50

   ;

   infile 'myfile.txt' dsd dlm='|' truncover lrecl=400 ;

   input FacilityName -- Fax ;

run;

View solution in original post


All Replies
Trusted Advisor
Posts: 1,141

Re: Importing Text File

Hi,

you can proc import on a very easy way.

I suggest you this publication: simple, and clear:

SAS FAQ: How do I read in a file that uses commas, tabs or spaces as delimiters to separate variable...

But you can also refer to the complete and official documentation:

http://support.sas.com/techsup/technote/ts673.pdf

Solution
‎11-24-2014 03:54 PM
Super User
Super User
Posts: 6,502

Re: Importing Text File

Copy your simple variable definitions and paste them into your program editor and turn them into a LENGTH statement.  The basically will involve adding the keyword LENGTH before, editing the names to be valid SAS variable names (getting rid of the embedded spaces), add a $ before the length for character strings (for numeric variables don't add the $ but do convert the "length" to 8 since SAS stores all numbers as 8 byte floating point numbers) and add a semicolon on the end.  Then just use it in a simple DATA step.

data want ;

  length

FacilityName $75

Address1 $50

Address2 $50

Address3 $50

City $50

State $2

ZipCode $15

Phone $50

Fax $50

   ;

   infile 'myfile.txt' dsd dlm='|' truncover lrecl=400 ;

   input FacilityName -- Fax ;

run;

Contributor
Posts: 24

Re: Importing Text File

Amazing! So simple. Thanks, Tom!

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 159 views
  • 0 likes
  • 3 in conversation