Hi SAS community;
I am running into an issue wherein I am unable to import a tab delimited file with inconsistent spacing. I am using the following codes to import the file but getting weird output. I have attached the file for your reference as well.
proc import datafile = '/folders/myfolders/MyRawData/base-guide-practice-data/cert/class.txt'
dbms=tab
out=pg1.class
replace;
delimiter = '09'x;
getnames=yes;
run;
Looking at the file you provided I can't see any inconsistency, but Notepad++ does not show any tabs, but spaces.
So switching to a data-step will be the easiest way to solve importing problems:
data pg1.class;
length
Name $ 10
Gender $ 1
Age 8
;
infile '/folders/myfolders/MyRawData/base-guide-practice-data/cert/class.txt' firstobs=2;
input @1 Name @10 Gender @19 Age;
run;
Good solution but I wanted to use proc import. truly appreciate you taking the time to reply.
Good solution but I wanted to use proc import. Truly appreciate you taking the time to reply.
For some reason, your file contains no tabs but spaces. Replace the spaces between fields with single tabs and your code will read the file just fine.
changed the codes as following but still not reading it properly.
PROC IMPORT DATAFILE = '/folders/myfolders/MyRawData/base-guide-practice-data/cert/class.txt'
OUT=CLASSEPORT
DBMS = dlm
REPLACE;
GETNAMES=YES;
DELIMITER = ' ';
RUN;
My suggestion was about changing the data, not the code.
what if the data size is big and changing the source isn't possible? I am more interested in knowing the solution of this particular challenge 🙂 otherwise, reading data by using input statement can do the magic as well.
Then reading the data along the lines suggested by @andreas_lds is the way to go. Proc import is meant to simplify your life when you meet more complicated situations.
If it is not tab delimited file ,using blank as delimiter .
proc import datafile = '/folders/myfolders/MyRawData/base-guide-practice-data/cert/class.txt'
dbms=dlm
out=pg1.class
replace;
delimiter = ' ' ;
getnames=yes;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.