title: How to pad out character fields in SAS?
body: <p>I am creating a SAS dataset from a database that includes a VARCHAR(5) key field.</p>
  
  <p>This field includes some entries that use all 5 characters and some that use fewer.</p>
  
  <p>When I import this data, I would prefer to pad all the shorter entries out to use all five characters. For this example, I want to pad on the left with <code>0</code>, the character zero. So, <code>114</code> would become <code>00114</code>, <code>ABCD</code> would become <code>0ABCD</code>, and <code>EA222</code> would stay as it is.</p>
  
  <p>I've attempted this with a simple data statement, but of course the following does not work:</p>
  
  <pre><code>data test;
      set databaseinput;
      format key $5.;
  run;
  </code></pre>
  
  <p>I've tried to do this with a user-defined informat, but I don't think it's possible to specify the ranges correctly on character fields, per <a href="http://support.sas.com/kb/7/570.html" rel="noreferrer">this SAS KB answer</a>. Plus, I'm fairly sure proc format won't let me define the result dynamically in terms of the incoming variable.</p>
  
  <p>I'm sure there's an obvious solution here, but I'm just missing it.</p>