Hello,
I get pathnames in a macro variable and I would like to extract the string between the 4th folder and the program name
exemple1, '/data/crea/creapil/sbsas/SUIVIS/ACCEPTATION/SUI_Q_DOS_PAI.sas' the result should be /SUIVIS/ACCEPTATION/
example 2 '/data/crea/creapil/sbsas/SUIVIS/CLIENT/RECOUV/CESSION/SUI_Q_CESSION.sas' the result should be SUIVIS/CLIENT/RECOUV/CESSION/
thanks in advance for your help
regards
Nas
Hi @Nasser_DRMCP What you need is CALL SCAN
data have;
length str $200;
str= '/data/crea/creapil/sbsas/SUIVIS/ACCEPTATION/SUI_Q_DOS_PAI.sas' ;
output;
str='/data/crea/creapil/sbsas/SUIVIS/CLIENT/RECOUV/CESSION/SUI_Q_CESSION.sas';
output;
run;
data want;
set have;
call scan(str, 5, _p, _l,'/');
call scan(str, -1, _p1, _l1,'/');
need=substr(str,_p-1,_p1+1-_p);
drop _:;
run;
Hi @Nasser_DRMCP What you need is CALL SCAN
data have;
length str $200;
str= '/data/crea/creapil/sbsas/SUIVIS/ACCEPTATION/SUI_Q_DOS_PAI.sas' ;
output;
str='/data/crea/creapil/sbsas/SUIVIS/CLIENT/RECOUV/CESSION/SUI_Q_CESSION.sas';
output;
run;
data want;
set have;
call scan(str, 5, _p, _l,'/');
call scan(str, -1, _p1, _l1,'/');
need=substr(str,_p-1,_p1+1-_p);
drop _:;
run;
could you please explain call scan(str, 5, _p, _l,'/'); ?
Basically you mentioned in your original post " I would like to extract the string between the 4th folder and the program name". This is the starting point to think through the logic. Given the start being obvious i.e. we begin from the position right after the 4th folder and that is 5th substr separated by '/'. We evaluate the position of the start of the 5th folder including from the point at which delimiter '/' begins. This is part 1.
Secondly we evaluate the end of the beginning of the program name using the 2nd call scan to exclude those characters. So the difference between starting position of program name and ending position of 4th folder is what we do using call scan.
Finally, we extract the substring of chars using the length value evaluated above starting from the ending position of the 4th folder. Hope this helps?
Novin's solution is good as long as you know what position to start the data from. I learnt something new today. Call Scan function, did not use it.
Alternatively with perl regular expression you can try below code
data want;
set have;
need=prxchange('s/(.*sbsas)(.*)(sui_q.*)/$2/oi',-1,str);
run;
Thanks
I am sorry , but the file name do not always begin with "SUI_Q" it was just examples. it could be INC_ or MON_ or...
sorry, I forgot to specify it.
and is it possible to use this command into a macro variable ?
thanks
in that case please try the below code
data want;
set have;
need=prxchange('s/(.*sbsas)(.*\/)(.*sas)/$2/oi',-1,str);
run;
sorry, in fact I can get the result in a macro variable by using call symput.
Sorry but there is someting left I don t understand.
why, the second group should be (.*\/) to obtain /SUIVIS/ACCEPTATION/
many thanks
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.