BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
vicky07
Quartz | Level 8

Hello,

 

I have a macro variable something like below and i want to remove the string after the last '\'. and store the output in macro

 

%let path = D:\input\sas\files\today\chart_data*.sas7bdat;

 

Expected output is:

 

D:\input\sas\files\today\

 

Basically i want to remove all the words after the last '\'. Can someone please help me to get the desired output? Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
%let path = D:\input\sas\files\today\chart_data*.sas7bdat;
%let want=%sysfunc(prxchange(s/[^\\\/]+$//,1,&path));

%put &want;

View solution in original post

7 REPLIES 7
LinusH
Tourmaline | Level 20
Take a look at the findc() and %substr() functions.
Data never sleeps
novinosrin
Tourmaline | Level 20

One easy and lazy way:

 

%let path = D:\input\sas\files\today\chart_data*.sas7bdat;

data _null_;
k="&path";
call scan(k, -1, position, length,'\');
substr(k,position,length)=' ';
call symputx('path',k);
run;

%put &path;

However, I challenge you to accomplish the same with %syscall with call scan in open code or rxchange

vicky07
Quartz | Level 8
I will give it a try with %syscall, thank you!
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You could double reverse it:

data want;
  pth=reverse(scan(reverse("&path."),1,"\"));
run;

Basically reverse the string, scna off the first block based on \ delimiter, then reverse the result.

vicky07
Quartz | Level 8
Got it, Thank You!
Ksharp
Super User
%let path = D:\input\sas\files\today\chart_data*.sas7bdat;
%let want=%sysfunc(prxchange(s/[^\\\/]+$//,1,&path));

%put &want;
vicky07
Quartz | Level 8
It worked. Thank You!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 7 replies
  • 1956 views
  • 2 likes
  • 5 in conversation