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 .
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,'_.');
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.