How to Put Leading Zeros in Front of an SAS Character Variable

Save

In SAS, all variables are either numeric or character. Character variables can consist of numbers, but they may be numbers that you wish to treat as characters. For example, ZIP codes are numeric and ID variables are often numeric, but, in most cases, the numeric quality is not interesting.

  • Create some character data. For example:

    data today;

    length x $10;

    input x $ @@;

    datalines;

    123 234 Robert

    ;

    run;

    Here, the length statement makes the variable x a character variable of length 10. Note that 123 and 234 are being read as character.

  • Decide how many leading zeros you want to add. For example, you might want to add two zeros.

  • Add the zeros by concatenating them. In SAS, the concatenation operator is ||. On most keyboards, this is on the upper right, in the shift position of the \ character. For example:

    data new;

    set today;

    y = '00'||x;

    run;

  • Check that it worked by printing the data.

    proc print data = new;

    run;

    Note that 00 is added to all cases.

Promoted By Zergnet

Comments

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!