Help using Base SAS procedures

Importing a text file with semicolons

Accepted Solution Solved
Reply
Regular Contributor
Posts: 192
Accepted Solution

Importing a text file with semicolons

Hello, any help you can give is  much appreciated!

I am trying to import a .txt file which has fields separated by semicolons.

When I use proc import the following proc import statement, the first column in the SAS table called 'FCode' which is a 14 digit code imports with a BEST format and is numeric and imports as a bunch of numbers with E in it.

But, I need the filed to be character and have leading zeroes (it has leading zeroes in the file).  How do you do that?  My code so far below -- Thanks!

proc IMPORT DATAFILE= "E:\enroll.txt"


DBMS=dlm


out=ethk1415



delimiter=';';



getnames=yes;

run;


Accepted Solutions
Solution
‎06-24-2015 09:40 PM
Super User
Super User
Posts: 6,498

Re: Importing a text file with semicolons

If nobody has given you any documentation on the fields in the file then you don't know how long the fields are.  But you could just make them longer than they need to be.

If you are reading a delimited file then you do NOT need to know where to start the next number.  The delimiters mark the transition between fields.

Do NOT use an INFORMAT statement to do the function of a LENGTH statement.  There is no need to assign an informat like $14 to a character variable. 

Why are your name fields different lengths? Don't they contain the same type of information?  Why are they named NAME NAME1 NAME2 instead of NAME1 NAME2 NAME3?

data enrl;

  infile "E:\enroll.txt" dsd dlm=";" TRUNCOVER firstobs=2;

  length code $14 name1-name3 $50 total 8;

  input code -- total;

run;

View solution in original post


All Replies
Super User
Posts: 17,750

Re: Importing a text file with semicolons

Look at your log. Take that code, which is a data step and modify it, since the data step gives you more control over your input data types.

Regular Contributor
Posts: 192

Re: Importing a text file with semicolons

Thank you both!  Appreciate your help!!

Q1)How do you know how long a field is in a .txt file?

Also, I tried the code below but some of the names (starting from name3) were cutoff so I'm thinking the 'input @' number is wrong.

Q2) But, how do you know where to start the next input # if you don't know how long the fields are? I tried importing into a MS Excel file and then imported into SAS. The field lengths were the following in this order in the .txt file.

code 14

name 50

name1 50

name2 17

total  12

data enrl;



infile "E:\enroll.txt"



dlm=";" missover firstobs=2;



informat code $14.


         name $50.


         name2 $50.


         name3 $17.


         total 12.;





input @1 code


          15 name


          65 name2


          115 name3


          132 total;


run

;

Solution
‎06-24-2015 09:40 PM
Super User
Super User
Posts: 6,498

Re: Importing a text file with semicolons

If nobody has given you any documentation on the fields in the file then you don't know how long the fields are.  But you could just make them longer than they need to be.

If you are reading a delimited file then you do NOT need to know where to start the next number.  The delimiters mark the transition between fields.

Do NOT use an INFORMAT statement to do the function of a LENGTH statement.  There is no need to assign an informat like $14 to a character variable. 

Why are your name fields different lengths? Don't they contain the same type of information?  Why are they named NAME NAME1 NAME2 instead of NAME1 NAME2 NAME3?

data enrl;

  infile "E:\enroll.txt" dsd dlm=";" TRUNCOVER firstobs=2;

  length code $14 name1-name3 $50 total 8;

  input code -- total;

run;

Frequent Contributor
Posts: 130

Re: Importing a text file with semicolons

As Reeza said, use a data step import.

data ethk1415;

infile "E:\enroll.txt"

dlm=";" missover firstobs=2;

informat var1 informat1. ....... varn informatn.;

format var1 format1. ....... varn formatn.;

input var1 ....... varn;

run;

Super User
Posts: 9,662

Re: Importing a text file with semicolons

Try another option   guessingrow=32767

Regular Contributor
Posts: 192

Re: Importing a text file with semicolons

Appreciate your help everyone!!Smiley Happy

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 423 views
  • 1 like
  • 5 in conversation