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;
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;
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.
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
;
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;
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;
Try another option guessingrow=32767
Appreciate your help everyone!!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.