<?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 0 to census tract value (character var) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Add-leading-0-to-census-tract-value-character-var/m-p/627008#M185003</link>
    <description>Just a quick note that if you want to save the results to the same variable the length statement needs to be before the SET statement. If you're creating a new variable this is not an issue.</description>
    <pubDate>Mon, 24 Feb 2020 20:27:47 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2020-02-24T20:27:47Z</dc:date>
    <item>
      <title>Add leading 0 to census tract value (character var)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-leading-0-to-census-tract-value-character-var/m-p/626991#M184994</link>
      <description>&lt;P&gt;I have a dataset that has census tract fips codes, but the leading 0 is getting cut off. I was able to put a leading 0 in front of the county fips code using substr and length, but the same thing is not working for census tract. I keep getting errors.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically, I have ct fips that look like this:&lt;/P&gt;
&lt;P&gt;1001020100&lt;/P&gt;
&lt;P&gt;1001020200&lt;/P&gt;
&lt;P&gt;24006840030&lt;/P&gt;
&lt;P&gt;24007850040&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And I want it to look like this (notice that the last two remain the same as they already have 11 digits);&lt;/P&gt;
&lt;P&gt;01001020100&lt;/P&gt;
&lt;P&gt;01001020200&lt;/P&gt;
&lt;P&gt;24006840030&lt;/P&gt;
&lt;P&gt;24007850040&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to leave these as character variables. In the code, I put&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ct = substr(ctfips, length(ctfips)-10, 11);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; drop ctfips;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; rename ct = ctfips;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Not sure if that's what I need to be doing, but it's not giving the leading zero for a total length of 11. Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 24 Feb 2020 19:47:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-leading-0-to-census-tract-value-character-var/m-p/626991#M184994</guid>
      <dc:creator>wernie</dc:creator>
      <dc:date>2020-02-24T19:47:17Z</dc:date>
    </item>
    <item>
      <title>Re: Add leading 0 to census tract value (character var)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-leading-0-to-census-tract-value-character-var/m-p/626992#M184995</link>
      <description>&lt;P&gt;Modified previous solution to fit what you are doing.&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/td-p/40486" target="_blank"&gt;https://communities.sas.com/t5/SAS-Procedures/how-to-pad-character-variable-with-leading-zeroes/td-p/40486&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
    length charvar $ 11 padded $ 11;
    infile datalines;
    input charvar;
    /* read charvar as number, then format with leading zeroes */
    /* then put result into padded */
    padded = put(input(charvar,best11.),z11.);
datalines;
1001020100
1001020200
24006840030
24007850040
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 24 Feb 2020 19:50:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-leading-0-to-census-tract-value-character-var/m-p/626992#M184995</guid>
      <dc:creator>Krueger</dc:creator>
      <dc:date>2020-02-24T19:50:45Z</dc:date>
    </item>
    <item>
      <title>Re: Add leading 0 to census tract value (character var)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-leading-0-to-census-tract-value-character-var/m-p/626994#M184997</link>
      <description>Where exactly in that code do you think you're adding a leading zero?</description>
      <pubDate>Mon, 24 Feb 2020 19:51:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-leading-0-to-census-tract-value-character-var/m-p/626994#M184997</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-02-24T19:51:19Z</dc:date>
    </item>
    <item>
      <title>Re: Add leading 0 to census tract value (character var)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-leading-0-to-census-tract-value-character-var/m-p/627004#M185002</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/44533"&gt;@wernie&lt;/a&gt;&amp;nbsp; The great Xia Keshan&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp; answered this a while ago&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input str $11.;
cards;
1001020100
1001020200
24006840030
24007850040
;

data want;
 set have;
 length want $11;
 want=translate(right(str),'0',' ');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 24 Feb 2020 20:11:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-leading-0-to-census-tract-value-character-var/m-p/627004#M185002</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-02-24T20:11:44Z</dc:date>
    </item>
    <item>
      <title>Re: Add leading 0 to census tract value (character var)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-leading-0-to-census-tract-value-character-var/m-p/627008#M185003</link>
      <description>Just a quick note that if you want to save the results to the same variable the length statement needs to be before the SET statement. If you're creating a new variable this is not an issue.</description>
      <pubDate>Mon, 24 Feb 2020 20:27:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-leading-0-to-census-tract-value-character-var/m-p/627008#M185003</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-02-24T20:27:47Z</dc:date>
    </item>
    <item>
      <title>Re: Add leading 0 to census tract value (character var)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-leading-0-to-census-tract-value-character-var/m-p/627009#M185004</link>
      <description>&lt;P&gt;Oh yes indeed. Honestly, I didn't pay attention to whether the OP wants the same input variable value to be modified rather than a new assignment. Very good and important point&lt;/P&gt;</description>
      <pubDate>Mon, 24 Feb 2020 20:36:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-leading-0-to-census-tract-value-character-var/m-p/627009#M185004</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-02-24T20:36:46Z</dc:date>
    </item>
    <item>
      <title>Re: Add leading 0 to census tract value (character var)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-leading-0-to-census-tract-value-character-var/m-p/627015#M185006</link>
      <description>&lt;P&gt;If the values can actually be converted to integers then you can use the Z format to convert it back with leading zeros.&amp;nbsp; So use INPUT() to convert to a number and PUT() to convert back to string.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ctfips = put(input(ctfips,11.),Z11.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now if the values either contain non-digit characters, or there are more than 15 characters then you will want to use character operations instead. Such as moving the spaces to the front and replacing them with zeros.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ctfips = translate(right(substr(ctfips,1,11)),'0',' ');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or prefixing zeros.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if length(ctfips) &amp;lt; 11 then ctfips = repeat('0',11-length(ctfips)-1)||ctfips;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ctfips = reverse(substr(reverse(cats(repeat('0',11-1),ctfips)),1,11));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Feb 2020 20:47:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-leading-0-to-census-tract-value-character-var/m-p/627015#M185006</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-24T20:47:02Z</dc:date>
    </item>
  </channel>
</rss>

