Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mf_trimstr.sas
Go to the documentation of this file.
1/**
2 @file mf_trimstr.sas
3 @brief Removes character(s) from the end, if they exist
4 @details If the designated characters exist at the end of the string, they
5 are removed
6
7 %put %mf_trimstr(/blah/,/); * /blah;
8 %put %mf_trimstr(/blah/,h); * /blah/;
9 %put %mf_trimstr(/blah/,h/);* /bla;
10
11 <h4> SAS Macros </h4>
12
13
14 @param basestr The string to be modified
15 @param trimstr The string to be removed from the end of `basestr`, if it
16 exists
17
18 @return output returns result with the value of `trimstr` removed from the end
19
20
21 @version 9.2
22 @author Allan Bowe
23
24**/
25
26%macro mf_trimstr(basestr,trimstr);
27%local baselen trimlen trimval;
28
29/* return if basestr is shorter than trimstr (or 0) */
30%let baselen=%length(%superq(basestr));
31%let trimlen=%length(%superq(trimstr));
32%if &baselen < &trimlen or &baselen=0 %then %return;
33
34/* obtain the characters from the end of basestr */
35%let trimval=%qsubstr(%superq(basestr)
36 ,%length(%superq(basestr))-&trimlen+1
37 ,&trimlen);
38
39/* compare and if matching, chop it off! */
40%if %superq(basestr)=%superq(trimstr) %then %do;
41 %return;
42%end;
43%else %if %superq(trimval)=%superq(trimstr) %then %do;
44 %qsubstr(%superq(basestr),1,%length(%superq(basestr))-&trimlen)
45%end;
46%else %do;
47 &basestr
48%end;
49
50%mend mf_trimstr;