BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Nasser_DRMCP
Lapis Lazuli | Level 10

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

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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;

View solution in original post

12 REPLIES 12
novinosrin
Tourmaline | Level 20

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;
Nasser_DRMCP
Lapis Lazuli | Level 10
thanks for your quick respons.
could you please explain call scan(str, 5, _p, _l,'/'); ?
novinosrin
Tourmaline | Level 20

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?

Nasser_DRMCP
Lapis Lazuli | Level 10
Thanks for your help and your reactivity
smantha
Lapis Lazuli | Level 10

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.

Jagadishkatam
Amethyst | Level 16

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,
Jag
Nasser_DRMCP
Lapis Lazuli | Level 10
Thanks Jagadishkatam . could you please explain ? thanks in advance
Jagadishkatam
Amethyst | Level 16
The perl regular expression works on the basis of meta characters which recognizes the patterns like digits, letters, special characters, so here we are using the

dot which means any character including the special character
* which means recognize the any character including the special character any number of times
sbsas is the word with which you want to end the pattern
() is group, to recognize a particular group pattern
so here we have 3 groups
s/ - substitution
$2 - keep the second group
-1 - recognize the pattern as it occurs 1 or more
o- retain the pattern to all records
i - ignore case
Thanks,
Jag
Nasser_DRMCP
Lapis Lazuli | Level 10

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

Jagadishkatam
Amethyst | Level 16

in that case please try the below code

 


data want;
 set have;
 need=prxchange('s/(.*sbsas)(.*\/)(.*sas)/$2/oi',-1,str);
run;
Thanks,
Jag
Jagadishkatam
Amethyst | Level 16
Could you please let me know how you want to use it in a macro variable, so i can help you
Thanks,
Jag
Nasser_DRMCP
Lapis Lazuli | Level 10

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

 

 

SAS Innovate 2025: Register Now

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!

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
  • 12 replies
  • 1275 views
  • 5 likes
  • 4 in conversation