Home › Forums › Main Forums › SAS Forum › Convert leading symbols to leading zeros
-
Convert leading symbols to leading zeros
-
data A;
length X $20;
input X $ ;
CARDS;
***5671
***32
***178
*******6454
****5678032
*************3890
;
run;
Data B;
length Y2 $20;
set A;
Y=tranwrd( strip(X), "*", "0");
run;Output: X
0005671
0032
000178
00000000064054
0005678032
00000000000000000038090TRANWRD Function: Replaces all occurrences of a substring in a character string.
Syntax
TRANWRD(source, target, replacement)
Required Arguments
source
specifies a character constant, variable, or expression that you want to translate.target
specifies a character constant, variable, or expression that is searched for in source.replacement
specifies a character constant, variable, or expression that replaces target. When the replacement string has a length of zero, TRANWRD uses a single blank instead.The Basics
The TRANWRD function copies the value in source to the result string while searching for all non-overlapping substrings in source that are equal to the value in target. Each of these substrings is omitted from the result and the value of replacement is copied in its place. The TRANWRD function does not remove trailing blanks in the target string or the replacement string.Comparisons
The TRANWRD function differs from the TRANSTRN function because TRANSTRN allows the replacement string to have a length of zero. TRANWRD uses a single blank instead when the replacement string has a length of zero.
The TRANSLATE function converts every occurrence of a user-supplied character to another character. TRANSLATE can scan for more than one character in a single call. However, in performing this scan, TRANSLATE searches for every occurrence of any of the individual characters within a string. That is, if any letter (or character) in the target string is found in the source string, it is replaced with the corresponding letter (or character) in the replacement string.
The TRANWRD function differs from TRANSLATE in that TRANWRD scans for substrings and replaces those substrings with a second substring.
Note: The order of arguments is different between the TRANWRD and TRANSLATE functions. In TRANWRD, the second argument is target and the third argument is replacement. In TRANSLATE, the second argument is to and the third argument is from. The character string specified by target is similar to the from argument in TRANSLATE. The character string specified by replacement is similar to the to argument in TRANSLATE.
Log in to reply.