Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
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 [in] basestr The string to be modified
15 @param [in] 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%let baselen=%length(%superq(basestr));
30%let trimlen=%length(%superq(trimstr));
31
32/* nothing to remove from nothing */
33%if &baselen=0 %then %return;
34
35%if &baselen < &trimlen %then %do;
36 /* a value shorter than the trim string cannot end with it, so it is
37 returned unchanged - returning nothing here lost the value */
38 &basestr
39%end;
40%else %do;
41 /* obtain the characters from the end of basestr */
42 %let trimval=%qsubstr(%superq(basestr)
43 ,&baselen-&trimlen+1
44 ,&trimlen);
45
46 /* compare and if matching, chop it off! */
47 %if %superq(basestr)=%superq(trimstr) %then %do;
48 %return;
49 %end;
50 %else %if %superq(trimval)=%superq(trimstr) %then %do;
51 %qsubstr(%superq(basestr),1,&baselen-&trimlen)
52 %end;
53 %else %do;
54 &basestr
55 %end;
56%end;
57
58%mend mf_trimstr;