DATA Step, Macro, Functions and more

Importing from Text file

Reply
Regular Contributor
Posts: 218

Importing from Text file

[ Edited ]

 

Student_ID                     Call_ID

09008767                       02541-027-0415471                                                                            
09149065                       04531-027-8565478                                                                            
09597360                       04531-027-2546587 

I have the above table in a .txt file. I want to import as sas dataset. My following code is not working. Can someone help me please. Please note that there are 23 character space between first two values and 14 space between 2nd and 3rd value (I have 9 variables total in the dataset). I am getting the following error message.

 

NOTE: The quoted string currently being processed has become more than 262 characters long. You
might have unbalanced quotation marks.

 

proc import datafile='R:\have.txt'
            out=test
            dbms=dlm
            replace;
	        delimiter='09'x;
run;

 

PROC Star
Posts: 307

Re: Importing from Text file

If there are spaces between the two fields, why are you using a tab ('09'x) as the delimiter?

Regular Contributor
Posts: 218

Re: Importing from Text file

Posted in reply to collinelliot
What should I use collinelliot?
Frequent Contributor
Posts: 80

Re: Importing from Text file

[ Edited ]

Use a data step instead, something like:

filename fileref 'filepath';

data test;
   infile fileref;
   input student_id 1-8 call_id $ 32-48;
run;

If that blank row is there, you could add a 'if _n_<=2 then delete;'

 

PROC Star
Posts: 7,471

Re: Importing from Text file

Or, rather than worrying about the specific column locations:

 

data want;
  infile '/folders/myfolders/have.txt' firstobs=3;
  informat Student_ID $8.;
  informat Call_ID $17.;
  input Student_ID Call_ID;
run;

HTH,

Art, CEO, AnalystFinder.com

 

Regular Contributor
Posts: 218

Re: Importing from Text file

I just noticed that each of my variable has different lengths with different space length in between. Is there a way I can import them without defining the character space. Like something that we do in imorting from Excel?

PROC Star
Posts: 7,471

Re: Importing from Text file

See my post above. Just make sure that the lengths set in the informats are long enough.

 

Art, CEO, AnalystFinder.com

Frequent Contributor
Posts: 80

Re: Importing from Text file

Depending on the variability... sometimes I cave and bring it all in as one giant field and then split with data step functions. For instance, if sometimes the first column value length is so long that it spills over to what is sometimes the 2nd column, then even specifying lengths on the informat isn't foolproof. So instead I (not saying it's the best way, just the crutch iI go to):

 

data test;
  infile;
  input BIG_VAR $ 1-250;

   var1=strip(scan(big_var,1," "));
   var2=strip(scan(big_var,2," "));
run;

Assuming that there are no spaces within the fields, this should split it up and then the strip function will get rid of the leading/trailing blanks I believe.

Super User
Posts: 10,023

Re: Importing from Text file

1)Try Arthur.T 's code and add one more  option.

data want;
  infile '/folders/myfolders/have.txt' firstobs=3 expandtabs;
  informat Student_ID $8.;
  informat Call_ID $17.;
  input Student_ID Call_ID;
run;


2)Try another engine.

proc import datafile='R:\have.txt'
            out=test
            dbms=tab
            replace;
	      
run;
Super User
Posts: 7,779

Re: Importing from Text file

Why use proc import at all with such an easy structure?

I copy/pasted your text data into a UNIX text file and ran the following:

data test;
infile '$HOME/sascommunity/test.txt' firstobs=3;
length student_id $8 call_id $17;
input student_id call_id;
run;

proc print data=test noobs;
run;

This is the result:

student_
   id            call_id

09008767    02541-027-0415471
09149065    04531-027-8565478
09597360    04531-027-2546587
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 9 replies
  • 258 views
  • 1 like
  • 6 in conversation