DATA Step, Macro, Functions and more

How to read non delimeted file ?

Accepted Solution Solved
Reply
Contributor
Posts: 29
Accepted Solution

How to read non delimeted file ?

warm greetings to all.

Can anyone help tell me how to read a text file with following format if there are 3 varibles: name age & city.

Text file : 

 

Rocky26network

Rosy54paris

Alberter28Portugal

 

version im using is latest university edition !

 

Thanks & Regards
Atul 


Accepted Solutions
Solution
‎06-06-2016 04:40 AM
Super Contributor
Posts: 259

Betreff: How to read non delimeted file ?

This is a very ugly data-format, but can be handled by using a regular expression:

 

data want;
   length rx 8
      Name $ 20 
      Age 8 
      City $ 100
   ;
   retain rx;
   drop rx;

   if _n_ = 1 then do;
      rx = prxparse('/([a-z]+)(\d+)([a-z]+)/i');
   end;
   

   input;

   if prxmatch(rx, trim(_infile_)) then do;
      Name = prxposn(rx, 1, trim(_infile_));
      Age = input(prxposn(rx, 2, trim(_infile_)), best.);
      City = prxposn(rx, 3, trim(_infile_));
   end;
   

datalines;
Rocky26network
Rosy54paris
Alberter28Portugal
;
run;

View solution in original post


All Replies
Contributor
Posts: 29

How to read non delimeted text file in sas ?

warm greetings to all.

Can anyone help tell me how to read a text file with following format if there are 3 varibles: name age & city.

Text file : 

 

Rocky26network

Rosy54paris

Alberter28Portugal

 

Thanks & Regards
Atul 

Solution
‎06-06-2016 04:40 AM
Super Contributor
Posts: 259

Betreff: How to read non delimeted file ?

This is a very ugly data-format, but can be handled by using a regular expression:

 

data want;
   length rx 8
      Name $ 20 
      Age 8 
      City $ 100
   ;
   retain rx;
   drop rx;

   if _n_ = 1 then do;
      rx = prxparse('/([a-z]+)(\d+)([a-z]+)/i');
   end;
   

   input;

   if prxmatch(rx, trim(_infile_)) then do;
      Name = prxposn(rx, 1, trim(_infile_));
      Age = input(prxposn(rx, 2, trim(_infile_)), best.);
      City = prxposn(rx, 3, trim(_infile_));
   end;
   

datalines;
Rocky26network
Rosy54paris
Alberter28Portugal
;
run;
Super User
Posts: 9,681

Re: How to read non delimeted file ?

data want;
length Name City $ 80;
input;
      Name = scan(_infile_,1,,'ka');
      Age = input(scan(_infile_,1,,'kd'), best.);
      City = scan(_infile_,2,,'ka');
datalines;
Rocky26network
Rosy54paris
Alberter28Portugal
;
run;
Super User
Super User
Posts: 7,401

Re: How to read non delimeted file ?

You may want to consider why the data is in that format in the first place, it isn't a good setup for a variety of reasons.  

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 285 views
  • 2 likes
  • 4 in conversation