<?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: how to pad character variable with leading zeroes? in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/m-p/40492#M10475</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;OK. Here are two solutions . Pick one up you like :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;data have;
input Raw $char6.;
cards;
007851 
05614&amp;nbsp; 
&amp;nbsp; 2447
;
run;

/*** the first way ***/
data want1;
 set have;
 want=translate(right(raw),'0',' ');
run;

/*** the second way ***/
data want2;
 set have;
 want=put(input(raw,best32.),z6.);
run;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Xia Keshan&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 21 Aug 2014 13:19:24 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2014-08-21T13:19:24Z</dc:date>
    <item>
      <title>how to pad character variable with leading zeroes?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/m-p/40486#M10469</link>
      <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
Could you please let me know any trick to pad character variable with leading zeroes?&lt;BR /&gt;
&lt;BR /&gt;
I have a 4 byte variable,if the data in that field is less than 4 bytes,then I would like to pad the value with leading zeroes.&lt;BR /&gt;
&lt;BR /&gt;
Please advice.</description>
      <pubDate>Tue, 29 Mar 2011 23:50:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/m-p/40486#M10469</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2011-03-29T23:50:15Z</dc:date>
    </item>
    <item>
      <title>Re: how to pad character variable with leading zeroes?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/m-p/40487#M10470</link>
      <description>&lt;P&gt;Your character variable value is all digit? Use INPUT to read as a number, then Z4. format to add leading zeroes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
    length charvar $ 4 padded $ 4;
    infile datalines;
    input charvar;
    /* read charvar as number, then format with leading zeroes */
    /* then put result into padded */
    padded = put(input(charvar,best4.),z4.);
datalines;
28
1034
4
8759
1234
234
34
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt; Output:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;charvar  padded

28       0028 
1034     1034 
4        0004 
8759     8759 
1234     1234 
234      0234 
34       0034 
&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt; Ksharp&lt;/P&gt;</description>
      <pubDate>Fri, 27 May 2016 13:57:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/m-p/40487#M10470</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-05-27T13:57:14Z</dc:date>
    </item>
    <item>
      <title>Re: how to pad character variable with leading zeroes?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/m-p/40488#M10471</link>
      <description>Thanks Kshrap!</description>
      <pubDate>Wed, 30 Mar 2011 15:16:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/m-p/40488#M10471</guid>
      <dc:creator>ren2010</dc:creator>
      <dc:date>2011-03-30T15:16:59Z</dc:date>
    </item>
    <item>
      <title>Re: how to pad character variable with leading zeroes?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/m-p/40489#M10472</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Ksharp&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In case I have a variable(character format) with both digits and characters, how will the code change?&lt;/P&gt;&lt;P&gt;For instance,&lt;/P&gt;&lt;P&gt;Raw data&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Desired output&lt;/P&gt;&lt;P&gt;007851&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 007851&lt;/P&gt;&lt;P&gt; 05614&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 005614 &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; 2447&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 002447&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Could you please help?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 20 Aug 2014 21:02:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/m-p/40489#M10472</guid>
      <dc:creator>namrata</dc:creator>
      <dc:date>2014-08-20T21:02:54Z</dc:date>
    </item>
    <item>
      <title>Re: how to pad character variable with leading zeroes?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/m-p/40490#M10473</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I just got the solution to my problem here:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://groups.google.com/forum/#!topic/comp.soft-sys.sas/uBuLYXMRqhw" title="https://groups.google.com/forum/#!topic/comp.soft-sys.sas/uBuLYXMRqhw"&gt;https://groups.google.com/forum/#!topic/comp.soft-sys.sas/uBuLYXMRqhw&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if length(raw data) = 6 then new=raw data;&lt;/P&gt;&lt;P&gt;else new=repeat('0',6-length(raw data)-1)||raw data;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Just thought would share the code here if anyone needs &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 20 Aug 2014 23:08:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/m-p/40490#M10473</guid>
      <dc:creator>namrata</dc:creator>
      <dc:date>2014-08-20T23:08:01Z</dc:date>
    </item>
    <item>
      <title>Re: how to pad character variable with leading zeroes?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/m-p/40491#M10474</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Namrata,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I believe your code will not produce&amp;nbsp; a desired output if the input records with length more than 6.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;can anyone suggests?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 21 Aug 2014 11:39:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/m-p/40491#M10474</guid>
      <dc:creator>RamKumar</dc:creator>
      <dc:date>2014-08-21T11:39:47Z</dc:date>
    </item>
    <item>
      <title>Re: how to pad character variable with leading zeroes?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/m-p/40492#M10475</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;OK. Here are two solutions . Pick one up you like :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;data have;
input Raw $char6.;
cards;
007851 
05614&amp;nbsp; 
&amp;nbsp; 2447
;
run;

/*** the first way ***/
data want1;
 set have;
 want=translate(right(raw),'0',' ');
run;

/*** the second way ***/
data want2;
 set have;
 want=put(input(raw,best32.),z6.);
run;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Xia Keshan&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 21 Aug 2014 13:19:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/m-p/40492#M10475</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2014-08-21T13:19:24Z</dc:date>
    </item>
    <item>
      <title>Re: how to pad character variable with leading zeroes?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/m-p/40493#M10476</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;A __default_attr="645292" __jive_macro_name="user" class="jive_macro jive_macro_user" data-objecttype="3" href="https://communities.sas.com/"&gt;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The first way seems magic &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;! So simple &amp;amp; yet effective!&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;@Ram Kumar My records are all less than 6.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 22 Aug 2014 01:33:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/m-p/40493#M10476</guid>
      <dc:creator>namrata</dc:creator>
      <dc:date>2014-08-22T01:33:11Z</dc:date>
    </item>
    <item>
      <title>Re: how to pad character variable with leading zeroes?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/m-p/328851#M62519</link>
      <description>&lt;P&gt;I have a similar problem. My zip variable data has following values:&lt;/P&gt;&lt;P&gt;zip&lt;/P&gt;&lt;P&gt;123456789&lt;/P&gt;&lt;P&gt;123456789&lt;/P&gt;&lt;P&gt;1234&lt;/P&gt;&lt;P&gt;1234&lt;/P&gt;&lt;P&gt;12345&lt;/P&gt;&lt;P&gt;123456789&lt;/P&gt;&lt;P&gt;12345&lt;/P&gt;&lt;P&gt;12345&lt;/P&gt;&lt;P&gt;1234&lt;/P&gt;&lt;P&gt;123456789&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to get a single variable with:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;first 5 digit from 9 digit zip var;&lt;/LI&gt;&lt;LI&gt;add leading zero where there is 4 digit zip and&lt;/LI&gt;&lt;LI&gt;keep five digit zip value as it is.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Thanking you in advance!&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>Tue, 31 Jan 2017 19:17:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/m-p/328851#M62519</guid>
      <dc:creator>pmpradhan</dc:creator>
      <dc:date>2017-01-31T19:17:12Z</dc:date>
    </item>
    <item>
      <title>Re: how to pad character variable with leading zeroes?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/m-p/328868#M62521</link>
      <description>&lt;P&gt;Found the answer to my question:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test2; set ptcz2;&lt;BR /&gt;zip5 = substr(compress(zip),1,5);&lt;BR /&gt;zip5v2 = substr(("0000"||zip5),length(zip5),5);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jan 2017 20:55:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/m-p/328868#M62521</guid>
      <dc:creator>pmpradhan</dc:creator>
      <dc:date>2017-01-31T20:55:19Z</dc:date>
    </item>
    <item>
      <title>Re: how to pad character variable with leading zeroes?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/m-p/424379#M68166</link>
      <description>&lt;P&gt;I have a similar problem. My data has following values:&lt;/P&gt;&lt;P&gt;zip&lt;/P&gt;&lt;P&gt;123456789&lt;/P&gt;&lt;P&gt;-123456789&lt;/P&gt;&lt;P&gt;1234&lt;/P&gt;&lt;P&gt;-.234&lt;/P&gt;&lt;P&gt;12345&lt;/P&gt;&lt;P&gt;-.23456789&lt;/P&gt;&lt;P&gt;12345&lt;/P&gt;&lt;P&gt;12345&lt;/P&gt;&lt;P&gt;1234&lt;/P&gt;&lt;P&gt;123456789&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to get a single variable with:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;first 6 digit from 9 digit zip var;&lt;/LI&gt;&lt;LI&gt;add leading zero where there is 1 less&amp;nbsp; digit zip and&lt;/LI&gt;&lt;LI&gt;keep 6 digit zip value as it is.&lt;/LI&gt;&lt;LI&gt;I tried increasing the zw.d format to 7.3 and its working for values with negative sign but the other values are not correct. Expected format is 6.3.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Thank you&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jan 2018 17:51:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/m-p/424379#M68166</guid>
      <dc:creator>Aidaan_10</dc:creator>
      <dc:date>2018-01-02T17:51:21Z</dc:date>
    </item>
  </channel>
</rss>

