<?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: Split a string with different size in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Split-a-string-with-different-size/m-p/460876#M117173</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data have;
 input var $;
 cards;
0157    
427     
;

data want;
set have;
if length(strip(var)) ne 5  then need=cats(substr(var,1,2),put(input(substr(var,3),8.),z3.));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 08 May 2018 22:25:10 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-05-08T22:25:10Z</dc:date>
    <item>
      <title>Split a string with different size</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-a-string-with-different-size/m-p/460871#M117172</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I wonder if anyone can help me on this issue. I need a code to split a string into two different string and then merge them again. I am working on a Compustat variable which is about zipcode. However, a lot of zipcode is incorrect (I guess). I want 5 digit zipcode (character) while some values have only 4 or 3 characters. My strategy is to slip values into two part: first part has left-most two digits, and second part has the rest. For second part, I need to add missing "0" (add 2 zeros if second part has only 1 character; and add 1 zero if second part has 2 character). Then I need to add first part and second part together to have full zipcode.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code should be generalised as most of zipcodes are correct with 5 digits (of course, I want to keep those zipcodes as they are).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is an example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Have&amp;nbsp; &amp;nbsp;Want&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;0157&amp;nbsp; &amp;nbsp;&amp;nbsp;01057&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;427&amp;nbsp; &amp;nbsp; &amp;nbsp;42007&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much!&lt;/P&gt;&lt;P&gt;Best regards,&lt;/P&gt;&lt;P&gt;Thierry&lt;/P&gt;</description>
      <pubDate>Tue, 08 May 2018 22:03:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-a-string-with-different-size/m-p/460871#M117172</guid>
      <dc:creator>tritringuyen</dc:creator>
      <dc:date>2018-05-08T22:03:23Z</dc:date>
    </item>
    <item>
      <title>Re: Split a string with different size</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-a-string-with-different-size/m-p/460876#M117173</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data have;
 input var $;
 cards;
0157    
427     
;

data want;
set have;
if length(strip(var)) ne 5  then need=cats(substr(var,1,2),put(input(substr(var,3),8.),z3.));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 08 May 2018 22:25:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-a-string-with-different-size/m-p/460876#M117173</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-05-08T22:25:10Z</dc:date>
    </item>
    <item>
      <title>Re: Split a string with different size</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-a-string-with-different-size/m-p/460882#M117175</link>
      <description>&lt;P&gt;An example creating a new zip variable so you can verify that the result is as needed looking at your old zip.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that the result for two characters is going to be wrong as you did not provide a rule for that case.&lt;/P&gt;
&lt;PRE&gt;data dummy;
   input zip $;
   if length(zip) &amp;lt; 5 then newzip=cats(substr(zip,1,2),put(input(substr(left(zip),3),best.),z3.));
   else newzip=zip;
datalines;
0157
427
12345
;
run;&lt;/PRE&gt;
&lt;P&gt;I used &amp;lt;5 in case your actual data has Zip+4 values in it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this works for your data you could replace the Newzip variable in the&amp;nbsp;IF statement with Zip and not need the else.&lt;/P&gt;</description>
      <pubDate>Tue, 08 May 2018 22:45:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-a-string-with-different-size/m-p/460882#M117175</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-05-08T22:45:18Z</dc:date>
    </item>
    <item>
      <title>Re: Split a string with different size</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-a-string-with-different-size/m-p/460884#M117176</link>
      <description>&lt;P&gt;Thank you very much! It really works!&lt;/P&gt;</description>
      <pubDate>Tue, 08 May 2018 22:59:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-a-string-with-different-size/m-p/460884#M117176</guid>
      <dc:creator>tritringuyen</dc:creator>
      <dc:date>2018-05-08T22:59:00Z</dc:date>
    </item>
    <item>
      <title>Re: Split a string with different size</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Split-a-string-with-different-size/m-p/460885#M117177</link>
      <description>Thank you very much! Yes, zipcodes may have 4 digits at the end, but I dont need it. But the code is helpful.</description>
      <pubDate>Tue, 08 May 2018 23:00:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Split-a-string-with-different-size/m-p/460885#M117177</guid>
      <dc:creator>tritringuyen</dc:creator>
      <dc:date>2018-05-08T23:00:56Z</dc:date>
    </item>
  </channel>
</rss>

