Type : CREATION_INTERNE
La macro opère uniquement sur des chaînes de caractères fournies en paramètres. Elle ne lit ni n'écrit de données externes ou de tables SAS. Elle utilise des fonctions macro pour la manipulation de chaînes.
| 1 | %macro mf_trimstr(basestr,trimstr); |
| 2 | %local baselen trimlen trimval; |
| 3 | |
| 4 | /* return if basestr is shorter than trimstr (or 0) */ |
| 5 | %let baselen=%LENGTH(%superq(basestr)); |
| 6 | %let trimlen=%LENGTH(%superq(trimstr)); |
| 7 | %IF &baselen < &trimlen or &baselen=0 %THEN %return; |
| 8 | |
| 9 | /* obtain the characters from the end of basestr */ |
| 10 | %let trimval=%qsubstr(%superq(basestr) |
| 11 | ,%LENGTH(%superq(basestr))-&trimlen+1 |
| 12 | ,&trimlen); |
| 13 | |
| 14 | /* compare and if matching, chop it off! */ |
| 15 | %IF %superq(basestr)=%superq(trimstr) %THEN %DO; |
| 16 | %return; |
| 17 | %END; |
| 18 | %ELSE %IF %superq(trimval)=%superq(trimstr) %THEN %DO; |
| 19 | %qsubstr(%superq(basestr),1,%LENGTH(%superq(basestr))-&trimlen) |
| 20 | %END; |
| 21 | %ELSE %DO; |
| 22 | &basestr |
| 23 | %END; |
| 24 | |
| 25 | %mend mf_trimstr; |