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

Written by peter flom
  • Share
  • Tweet
  • Share
  • Email

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, Postcodes are numeric and ID variables are often numeric, but, in most cases, the numeric quality is not interesting.

Skill level:
Moderately Easy


  1. 1

    Create some character data. For example:

    data today;

    length x £6;

    input x $ @@;


    123 234 Robert



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

  2. 2

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

  3. 3

    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;


  4. 4

    Check that it worked by printing the data.

    proc print data = new;


    Note that 00 is added to all cases.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.