Home Forums Main Forums SAS Forum Convert leading symbols to leading zeros

  • Convert leading symbols to leading zeros

     Datura updated 3 years, 9 months ago 1 Member · 1 Post
  • Datura

    Member
    February 3, 2021 at 12:39 pm
    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
    00000000000000000038090

    TRANWRD Function: Replaces all occurrences of a substring in a character string.

    https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=lefunctionsref&docsetTarget=p0pgemqcslm9uen1tvr5gcrusgrw.htm&locale=en

    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.

    • This discussion was modified 3 years, 9 months ago by  Datura.
    • This discussion was modified 3 years, 9 months ago by  Datura.

Log in to reply.

Original Post
0 of 0 posts June 2018
Now