BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jcis7
Pyrite | Level 9

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

6 REPLIES 6
Reeza
Super User

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.

jcis7
Pyrite | Level 9

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

;

Tom
Super User Tom
Super User

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;

dcruik
Lapis Lazuli | Level 10

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;

Ksharp
Super User

Try another option   guessingrow=32767

jcis7
Pyrite | Level 9

Appreciate your help everyone!!Smiley Happy

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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