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, ".");

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1169 views
  • 0 likes
  • 4 in conversation