How to remove words after '\' in a string?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 82
Accepted Solution

How to remove words after '\' in a string?

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.


Accepted Solutions
Solution
‎04-27-2018 09:24 AM
Super User
Posts: 10,788

Re: How to remove words after '\' in a string?

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

%put &want;

View solution in original post


All Replies
Super User
Posts: 5,890

Re: How to remove words after '\' in a string?

Take a look at the findc() and %substr() functions.
Data never sleeps
PROC Star
Posts: 1,852

Re: How to remove words after '\' in a string?

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

Frequent Contributor
Posts: 82

Re: How to remove words after '\' in a string?

Posted in reply to novinosrin
I will give it a try with %syscall, thank you!
Super User
Super User
Posts: 9,617

Re: How to remove words after '\' in a string?

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.

Frequent Contributor
Posts: 82

Re: How to remove words after '\' in a string?

Got it, Thank You!
Solution
‎04-27-2018 09:24 AM
Super User
Posts: 10,788

Re: How to remove words after '\' in a string?

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

%put &want;
Frequent Contributor
Posts: 82

Re: How to remove words after '\' in a string?

It worked. Thank You!
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 196 views
  • 2 likes
  • 5 in conversation