<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Add Leading 0s to String in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Add-Leading-0s-to-String/m-p/703911#M79943</link>
    <description>&lt;P&gt;This is a frequently asked question.&lt;/P&gt;
&lt;P&gt;If you are positive the string form valid integers of less than 15 digits then you can first convert the string to a number and convert it back to string using the Z format so that leading zeros are written.&amp;nbsp; Integers longer than that cannot be continuously exactly represented as numbers in SAS because it use floating point numbers.&amp;nbsp; The INPUT() function doesn't care if you use too long a width on the informat and the normal numeric informat has a maximum width of 32.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;string=put(input(string,32.),Z6.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Otherwise calculate the length and prefix the needed number of 0 digits. There are a lots of ways to do that. Here is one using the REPEAT() function to generate the string of zeros that is pretty easy to understand. If the length is already 6 (or greater) then you don't need any extra characters. Otherwise you will need to add (6-length of the string) number of zeros. The repeat function count argument requires 1 less than the length you want (think of it as the number or EXTRA times to repeat the pattern).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if length(string)&amp;lt;6 then string=cats(repeat('0',6-length(string)-1),string);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 06 Dec 2020 05:15:43 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2020-12-06T05:15:43Z</dc:date>
    <item>
      <title>Add Leading 0s to String</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Add-Leading-0s-to-String/m-p/703154#M79920</link>
      <description>&lt;P&gt;I have a variable&amp;nbsp; that is string but contains all numeric values. Examples of values include: 51, 200, 10511. I want to keep this a string variable, but have leading 0s that make each value 6 characters--specifically, such that the former values become 000051, 000200, and 010511. Can anyone help think of an elegant way to do this?&lt;/P&gt;</description>
      <pubDate>Wed, 02 Dec 2020 17:07:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Add-Leading-0s-to-String/m-p/703154#M79920</guid>
      <dc:creator>raivester</dc:creator>
      <dc:date>2020-12-02T17:07:28Z</dc:date>
    </item>
    <item>
      <title>Re: Add Leading 0s to String</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Add-Leading-0s-to-String/m-p/703156#M79921</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;I have a variable&amp;nbsp; that is string but contains all numeric values.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This is really poorly worded. A SAS variable is either numeric or character. It doesn't matter if it "contains all numeric values", it still can be either numeric or character.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming you mean your variable is NUMERIC:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use the Z. format. Example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
    y=50232;
    z=put(y,z6.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Dec 2020 17:13:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Add-Leading-0s-to-String/m-p/703156#M79921</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-12-02T17:13:08Z</dc:date>
    </item>
    <item>
      <title>Re: Add Leading 0s to String</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Add-Leading-0s-to-String/m-p/703157#M79922</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;string = put(input(string,best.),z6.-l);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The -l makes it left-aligned (no leading blanks).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Dec 2020 17:16:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Add-Leading-0s-to-String/m-p/703157#M79922</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-12-02T17:16:49Z</dc:date>
    </item>
    <item>
      <title>Re: Add Leading 0s to String</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Add-Leading-0s-to-String/m-p/703158#M79923</link>
      <description>&lt;P&gt;Sorry--I meant that my variable is character (although&amp;nbsp; each value contains only digits). I want to make it such that they all have a length of 6, by adding leading 0s to those with a length that has fewer than 6 characters (which is all values). I want the resultant variable to remain character.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Dec 2020 17:17:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Add-Leading-0s-to-String/m-p/703158#M79923</guid>
      <dc:creator>raivester</dc:creator>
      <dc:date>2020-12-02T17:17:31Z</dc:date>
    </item>
    <item>
      <title>Re: Add Leading 0s to String</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Add-Leading-0s-to-String/m-p/703910#M79942</link>
      <description>&lt;PRE&gt;data want;&lt;BR /&gt;  length z $6;&lt;BR /&gt;  do z = '1', '22', '333', '4444', '55555', '666666', 'A';&lt;BR /&gt;    zs = substr('000000' || left(z), length(left(z))+1);&lt;BR /&gt;    output;&lt;BR /&gt;  end;&lt;BR /&gt;run; &lt;/PRE&gt;</description>
      <pubDate>Sun, 06 Dec 2020 04:49:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Add-Leading-0s-to-String/m-p/703910#M79942</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-12-06T04:49:44Z</dc:date>
    </item>
    <item>
      <title>Re: Add Leading 0s to String</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Add-Leading-0s-to-String/m-p/703911#M79943</link>
      <description>&lt;P&gt;This is a frequently asked question.&lt;/P&gt;
&lt;P&gt;If you are positive the string form valid integers of less than 15 digits then you can first convert the string to a number and convert it back to string using the Z format so that leading zeros are written.&amp;nbsp; Integers longer than that cannot be continuously exactly represented as numbers in SAS because it use floating point numbers.&amp;nbsp; The INPUT() function doesn't care if you use too long a width on the informat and the normal numeric informat has a maximum width of 32.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;string=put(input(string,32.),Z6.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Otherwise calculate the length and prefix the needed number of 0 digits. There are a lots of ways to do that. Here is one using the REPEAT() function to generate the string of zeros that is pretty easy to understand. If the length is already 6 (or greater) then you don't need any extra characters. Otherwise you will need to add (6-length of the string) number of zeros. The repeat function count argument requires 1 less than the length you want (think of it as the number or EXTRA times to repeat the pattern).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if length(string)&amp;lt;6 then string=cats(repeat('0',6-length(string)-1),string);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 06 Dec 2020 05:15:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Add-Leading-0s-to-String/m-p/703911#M79943</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-12-06T05:15:43Z</dc:date>
    </item>
    <item>
      <title>Re: Add Leading 0s to String</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Add-Leading-0s-to-String/m-p/706931#M79973</link>
      <description>&lt;P&gt;Yet another way to do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;string=translate(right(string),'0',' ');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This assumes that the string is defined with length 6. If not, you can use the SUBSTR function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;string=translate(right(substr(string,1,6)),'0',' ');&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 18 Dec 2020 12:22:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Add-Leading-0s-to-String/m-p/706931#M79973</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-12-18T12:22:00Z</dc:date>
    </item>
  </channel>
</rss>

