<?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: converting numeric zip codes of varying lengths in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/converting-numeric-zip-codes-of-varying-lengths/m-p/966759#M376217</link>
    <description>Why do you have numeric zipcodes in the first place? This has to be a faulty import process.</description>
    <pubDate>Sat, 17 May 2025 07:53:21 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2025-05-17T07:53:21Z</dc:date>
    <item>
      <title>converting numeric zip codes of varying lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/converting-numeric-zip-codes-of-varying-lengths/m-p/966731#M376207</link>
      <description>&lt;P&gt;I've got numeric zip code values of various length.&amp;nbsp; &amp;nbsp;I used z format in the example below.&amp;nbsp; &amp;nbsp;It worked for value that was 2 digits but I got weird results for the ones longer than 5.&amp;nbsp; Is there a better way?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;data sample;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;input num_zip 8.;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;format num_zip 9.;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;put _all_;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;/*read un numeric values of various lengths*/&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;datalines;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;23&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;7777777&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;999999999&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;data convert;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;set sample;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;zipcode=put(num_zip,z5.);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;proc print data=convert noobs;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;var zipcode;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;run;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&amp;nbsp;zipcode &lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;00023&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;778E4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;001E7&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 16 May 2025 20:19:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/converting-numeric-zip-codes-of-varying-lengths/m-p/966731#M376207</guid>
      <dc:creator>Batman</dc:creator>
      <dc:date>2025-05-16T20:19:38Z</dc:date>
    </item>
    <item>
      <title>Re: converting numeric zip codes of varying lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/converting-numeric-zip-codes-of-varying-lengths/m-p/966746#M376210</link>
      <description>&lt;P&gt;First question is what do you want as the result?&amp;nbsp; Should the longer values be rounded:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;77778&lt;/P&gt;
&lt;P&gt;10000&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or should they be truncated:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;77777&lt;/P&gt;
&lt;P&gt;99999&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming they should be truncated, you would need to change the values before applying a format (such as z5.):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set sample;
if num_zip &amp;gt; 99999 then do until (num_zip &amp;lt;= 99999);
   *Yes, there are more compact ways to do this;
   num_zip = int(num_zip / 10);
end;
zipcode = put(num_zip, z5.);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 16 May 2025 21:59:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/converting-numeric-zip-codes-of-varying-lengths/m-p/966746#M376210</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2025-05-16T21:59:27Z</dc:date>
    </item>
    <item>
      <title>Re: converting numeric zip codes of varying lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/converting-numeric-zip-codes-of-varying-lengths/m-p/966755#M376214</link>
      <description>&lt;P&gt;Several of the SAS formats will attempt to yield "something" that comes close to a representation of a value when your width doesn't allow full display of the value. Presence of an E in numeric value means scientific notation has been applied. So when you attempt to fit a value with more than 5 digits, such as 7777777 into 5 characters then you get things like 778e4 because that's is about as close as you can get to the 7 digits using 5 characters. You would get something similar with small values like 0.0000000001234 only the exponent, the part after E would be negative.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Question, if you only want 5 character zips why did you bother to read in more than 5 digits? You won't have the problem&amp;nbsp; with&lt;/P&gt;
&lt;PRE&gt;data sample;
input num_zip 5.;
format num_zip z5.;
datalines;
23
7777777
999999999
;
run;&lt;/PRE&gt;
&lt;P&gt;Personally I &lt;STRONG&gt;never&lt;/STRONG&gt; read ZIP codes as numeric because the modern zip codes may be&amp;nbsp;&lt;STRONG&gt;10&lt;/STRONG&gt; characters long ddddd-dddd with a dash in the middle (and I have them). Which would read incorrectly in with a numeric informat.&lt;/P&gt;</description>
      <pubDate>Sat, 17 May 2025 03:49:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/converting-numeric-zip-codes-of-varying-lengths/m-p/966755#M376214</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2025-05-17T03:49:37Z</dc:date>
    </item>
    <item>
      <title>Re: converting numeric zip codes of varying lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/converting-numeric-zip-codes-of-varying-lengths/m-p/966759#M376217</link>
      <description>Why do you have numeric zipcodes in the first place? This has to be a faulty import process.</description>
      <pubDate>Sat, 17 May 2025 07:53:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/converting-numeric-zip-codes-of-varying-lengths/m-p/966759#M376217</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-05-17T07:53:21Z</dc:date>
    </item>
    <item>
      <title>Re: converting numeric zip codes of varying lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/converting-numeric-zip-codes-of-varying-lengths/m-p/966944#M376242</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/9248"&gt;@Batman&lt;/a&gt;&amp;nbsp;, when you use &lt;EM&gt;z.&lt;/EM&gt; format to display a number, it will be displayed in a way like this: 1) the number will start with several &lt;EM&gt;0&lt;/EM&gt;, depending on how many digits the &lt;EM&gt;z.&lt;/EM&gt; format has, 2) to determine how many digits the z. format has, use a number after &lt;EM&gt;z before the dot .&lt;/EM&gt;, for example, &lt;EM&gt;z5.&lt;/EM&gt; means it has five digits with several 0 before your number, if your number has 2 digits, it will has three 0 before it, if you number has 4 digits, it will has one 0 before it, and 3) if you use &lt;EM&gt;z5.&lt;/EM&gt; for a number longer than 5 digits, it won't work. So to solve your question, you need a number larger than 5 in the&amp;nbsp;&lt;EM&gt;z.&lt;/EM&gt; format, for example, &lt;EM&gt;z10.&lt;/EM&gt;, which gives more space to display your number. The code and output is as follows:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sample;
   input num_zip 8.;
   format num_zip 9.;
   datalines;
23
7777777
999999999
;
run;
data convert;
   set sample;
   zipcode=put(num_zip,z10.);
run;
proc print data=convert noobs;
   var zipcode;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_0-1747663371916.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107153i92F1010FA8439871/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_0-1747663371916.png" alt="dxiao2017_0-1747663371916.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 19 May 2025 14:06:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/converting-numeric-zip-codes-of-varying-lengths/m-p/966944#M376242</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2025-05-19T14:06:25Z</dc:date>
    </item>
    <item>
      <title>Re: converting numeric zip codes of varying lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/converting-numeric-zip-codes-of-varying-lengths/m-p/966945#M376243</link>
      <description>&lt;P&gt;US Zip codes?&amp;nbsp; Those are either 5 digits or 5+4 digits.&amp;nbsp; So you can probably just test if the value is larger than 99,999 to tell which type you have.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;20   data sample;
21     input num_zip ;
22     if num_zip &amp;gt; 99999 then zip=put(int(num_zip/10000),Z5.);
23     else zip=put(num_zip,Z5.);
24     put (_all_) (=);
25   datalines;

num_zip=23 zip=00023
num_zip=7777777 zip=00777
num_zip=999999999 zip=99999
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 19 May 2025 14:34:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/converting-numeric-zip-codes-of-varying-lengths/m-p/966945#M376243</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-05-19T14:34:53Z</dc:date>
    </item>
    <item>
      <title>Re: converting numeric zip codes of varying lengths</title>
      <link>https://communities.sas.com/t5/SAS-Programming/converting-numeric-zip-codes-of-varying-lengths/m-p/966946#M376244</link>
      <description>&lt;P&gt;Even if they had been imported as character, there would still be a problem, since we need different methods to convert them to five digit zips based on the length of original variable.&lt;/P&gt;</description>
      <pubDate>Mon, 19 May 2025 14:41:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/converting-numeric-zip-codes-of-varying-lengths/m-p/966946#M376244</guid>
      <dc:creator>Batman</dc:creator>
      <dc:date>2025-05-19T14:41:03Z</dc:date>
    </item>
  </channel>
</rss>

