Type : CREATION_INTERNE
La macro opera únicamente sobre cadenas de caracteres proporcionadas como parámetros. No lee ni escribe datos externos o tablas SAS. Utiliza funciones macro para la manipulación de cadenas.
| 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; |