BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Attyslogin
Obsidian | Level 7

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 

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

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

4 REPLIES 4
Attyslogin
Obsidian | Level 7

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 

andreas_lds
Jade | Level 19

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;
Ksharp
Super User
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;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.  

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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