<?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: leading zeroes in character variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/leading-zeroes-in-character-variable/m-p/438496#M109343</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/91713"&gt;@GinaRepole&lt;/a&gt;&amp;nbsp; Zn.&amp;nbsp; is a FORMAT, which is how your code is using it.&lt;/P&gt;</description>
    <pubDate>Mon, 19 Feb 2018 21:10:07 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2018-02-19T21:10:07Z</dc:date>
    <item>
      <title>leading zeroes in character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/leading-zeroes-in-character-variable/m-p/438485#M109337</link>
      <description>&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; want;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;input&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; ln_no $ ;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;datalines&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;21555555&lt;/P&gt;
&lt;P&gt;5544000&lt;/P&gt;
&lt;P&gt;6654444&lt;/P&gt;
&lt;P&gt;1145555555555&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; want2;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; want;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/*if ln_no lt 10 THEN LN_No = substr(left(LN_Nbr_R),1,10.);*/&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; ln_no lt &lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;10&lt;/FONT&gt;&lt;/STRONG&gt; &lt;FONT color="#0000ff" face="Courier New" size="3"&gt;THEN&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; LN_No = &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;put&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;(ln_no, &lt;/FONT&gt;&lt;FONT color="#008080" face="Courier New" size="3"&gt;z10.&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080" face="Courier New" size="3"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am attempting to add leading zeroes sufficient to make the character variable ln_no have 10 characters. ie 21555555 becomes 0021555555&amp;nbsp;&amp;nbsp; if the variable is 10 or more characters, I want to bring it in as is.&amp;nbsp; Above is the code I have tried but no success&lt;/P&gt;</description>
      <pubDate>Mon, 19 Feb 2018 20:38:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/leading-zeroes-in-character-variable/m-p/438485#M109337</guid>
      <dc:creator>Q1983</dc:creator>
      <dc:date>2018-02-19T20:38:27Z</dc:date>
    </item>
    <item>
      <title>Re: leading zeroes in character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/leading-zeroes-in-character-variable/m-p/438492#M109340</link>
      <description>&lt;P&gt;Why are you comparing your character variable to an integer constant?&amp;nbsp; Why are trying to apply a numeric format to your character variable?&amp;nbsp; One of your example values is longer than 10 characters?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the values are valid integers then convert them to numbers and use the Z format to produce the value with the leading zeros.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have 
  infile cards truncover ;
  input ln_no $20. ;
datalines;

21555555
5544000
6654444
1145555555555
;

data want;
  set have ;
  if 0 &amp;lt; lengthn(ln_no) &amp;lt;10 then ln_no=put(input(ln_no,32.),Z10.);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs        ln_no

 1
 2     0021555555
 3     0005544000
 4     0006654444
 5     1145555555555&lt;/PRE&gt;</description>
      <pubDate>Mon, 19 Feb 2018 21:07:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/leading-zeroes-in-character-variable/m-p/438492#M109340</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-02-19T21:07:51Z</dc:date>
    </item>
    <item>
      <title>Re: leading zeroes in character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/leading-zeroes-in-character-variable/m-p/438493#M109341</link>
      <description>&lt;P&gt;Do like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input ln_no $;
datalines;
21555555
5544000
6654444
1145555555555
;
run;

data want2;
set want;
/*if ln_no lt 10 THEN LN_No = substr(left(LN_Nbr_R),1,10.);*/
ln_no=put(input(ln_no,best10.),z10.);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 19 Feb 2018 21:07:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/leading-zeroes-in-character-variable/m-p/438493#M109341</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-02-19T21:07:59Z</dc:date>
    </item>
    <item>
      <title>Re: leading zeroes in character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/leading-zeroes-in-character-variable/m-p/438494#M109342</link>
      <description>&lt;P&gt;You can use the Zw.d format to add leading zeros, so I might suggest reading your data as numeric first, then converting over to character. There may be more elegant solutions, but here's an idea that seems to accomplish what you're asking for:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	input ln_no;
	ln_no2 = PUT(ln_no, Z16.);
	
	DO i=1 TO 6;
		IF CHAR(ln_no2, i) = 0 THEN
			trim = i+1;
		ELSE LEAVE;
	END;

	ln_no2 = SUBSTR(ln_no2, trim);

datalines;
21555555
5544000
6654444
1145555555555
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This solution pads all values up to 16 characters with leading 0s (16 is the max length of a number on Windows SAS&amp;nbsp;so this should be sufficient). Any extra zeros are then trimmed off, but not allowing the character string to go below a length of 10.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 Feb 2018 21:20:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/leading-zeroes-in-character-variable/m-p/438494#M109342</guid>
      <dc:creator>GinaRepole</dc:creator>
      <dc:date>2018-02-19T21:20:22Z</dc:date>
    </item>
    <item>
      <title>Re: leading zeroes in character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/leading-zeroes-in-character-variable/m-p/438496#M109343</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/91713"&gt;@GinaRepole&lt;/a&gt;&amp;nbsp; Zn.&amp;nbsp; is a FORMAT, which is how your code is using it.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Feb 2018 21:10:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/leading-zeroes-in-character-variable/m-p/438496#M109343</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-02-19T21:10:07Z</dc:date>
    </item>
    <item>
      <title>Re: leading zeroes in character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/leading-zeroes-in-character-variable/m-p/438500#M109346</link>
      <description>Whoops! Good catch. I updated the above post to be accurate.</description>
      <pubDate>Mon, 19 Feb 2018 21:21:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/leading-zeroes-in-character-variable/m-p/438500#M109346</guid>
      <dc:creator>GinaRepole</dc:creator>
      <dc:date>2018-02-19T21:21:02Z</dc:date>
    </item>
    <item>
      <title>Re: leading zeroes in character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/leading-zeroes-in-character-variable/m-p/438628#M109383</link>
      <description>&lt;P&gt;If your data is character, you may as well treat is as such, instead of messing round with numeric formats:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
length ln_no $20;
input ln_no $ ;
datalines;
21555555
5544000
6654444
1145555555555
;run;

data want;
  set have;
  if lengthn(ln_no)&amp;lt;10 then
    ln_no=repeat('0',9-lengthn(ln_no))!!ln_no;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I used the LENGTHN function because the LENGTH function will report a length of 1 for an empty string (LENGTHN returns 0).&lt;/P&gt;</description>
      <pubDate>Tue, 20 Feb 2018 13:04:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/leading-zeroes-in-character-variable/m-p/438628#M109383</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-02-20T13:04:05Z</dc:date>
    </item>
    <item>
      <title>Re: leading zeroes in character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/leading-zeroes-in-character-variable/m-p/439304#M109614</link>
      <description>&lt;P&gt;&lt;img id="robotwink" class="emoticon emoticon-robotwink" src="https://communities.sas.com/i/smilies/16x16_robot-wink.png" alt="Robot wink" title="Robot wink" /&gt;&lt;/P&gt;&lt;P&gt;Here's what I do...&lt;/P&gt;&lt;P&gt;/*REFORMAT NUMBER WITH LEADING ZEROS*/&lt;BR /&gt;format NEW_NUM $10. ;&lt;BR /&gt;SWAP = NUMBER;&lt;BR /&gt;NEW_NUM = put(SWAP, z10.);/*ADD LEADING ZEROS TO NEW_NUM*/&lt;/P&gt;</description>
      <pubDate>Thu, 22 Feb 2018 14:53:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/leading-zeroes-in-character-variable/m-p/439304#M109614</guid>
      <dc:creator>mich1</dc:creator>
      <dc:date>2018-02-22T14:53:26Z</dc:date>
    </item>
  </channel>
</rss>

