BookmarkSubscribeRSS Feed
awais
Obsidian | Level 7

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;

9 REPLIES 9
andreas_lds
Jade | Level 19

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;
awais
Obsidian | Level 7

Good solution but I wanted to use proc import. truly appreciate you taking the time to reply.

awais
Obsidian | Level 7

Good solution but I wanted to use proc import. Truly appreciate you taking the time to reply.

PGStats
Opal | Level 21

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.

PG
awais
Obsidian | Level 7

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;

PGStats
Opal | Level 21

My suggestion was about changing the data, not the code.

PG
awais
Obsidian | Level 7

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.

PGStats
Opal | Level 21

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.

PG
Ksharp
Super User

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 9 replies
  • 2688 views
  • 0 likes
  • 4 in conversation