BookmarkSubscribeRSS Feed
Midi
Obsidian | Level 7

Hi everyone , i have this file name:

20191102_Registre_Distributions_isan.csv; 

and i want to extract the first part which would be ' Registre_Distributions ' and the second part which would be ' isan '  , and i'm wondering how it could be achieved using the prxchange function.

any help would be much appreciated , thank you .

3 REPLIES 3
Tom
Super User Tom
Super User

To write a regular expression you need rules. What are the rules that describe how the filenames are constructed? And what parts you want to extract?

 

The easiest would be if the string always consists of four parts separated by underscore. Then there would be no need for regular expressions at all. Simple SCAN() function will work.

first_part = catx(('_',scan(filename,2,'_'),scan(filename(3,'_'));
second_part = scan(filename,4,'_.');
PGStats
Opal | Level 21

Assuming that the file name consists of a numeric field followed by an underscore, and then an alphanumeric field, an underscore, and an alphabetic word:

 

data _null_;
txt = "20191102_Registre_Distributions_isan.csv";
firstþart = prxchange("s/(\d+_)(\w+)_([a-z]+)\.csv/\2/io", 1, txt);
secondPart = prxchange("s/(\d+_)(\w+)_([a-z]+)\.csv/\3/io", 1, txt);
put (_all_) (=/);
run;
 txt=20191102_Registre_Distributions_isan.csv
 firstþart=Registre_Distributions
 secondPart=isan
PG
Reeza
Super User
SCAN() is infinitely easier.

file_name = scan (var_name, 1, ".");
ext = scan(var_name, 2, ".");

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 627 views
  • 0 likes
  • 4 in conversation