<?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 Compress and leading zeros if needed in datastep in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Compress-and-leading-zeros-if-needed-in-datastep/m-p/755424#M238396</link>
    <description>&lt;PRE&gt;data have;
length ssn $9;
input ssn;
datalines;
000000000
112334588
426-277
;run; &lt;/PRE&gt;
&lt;P&gt;Creates the following output&lt;/P&gt;
&lt;P&gt;ssn&lt;BR /&gt;000000000&lt;BR /&gt;112334588&lt;BR /&gt;426-277&lt;/P&gt;
&lt;P&gt;I need to do the following;&lt;/P&gt;
&lt;P&gt;if the ssn is a blank or if there is any character except consecutive 9 numbers I need it to show as 000000000&amp;nbsp; where this is a character&lt;/P&gt;
&lt;P&gt;Does compress work in this situation?&amp;nbsp; I have not really used compress much.&amp;nbsp; So in the example here I need for 426-277 to read 000426277&lt;/P&gt;</description>
    <pubDate>Tue, 20 Jul 2021 17:39:06 GMT</pubDate>
    <dc:creator>Q1983</dc:creator>
    <dc:date>2021-07-20T17:39:06Z</dc:date>
    <item>
      <title>Compress and leading zeros if needed in datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compress-and-leading-zeros-if-needed-in-datastep/m-p/755424#M238396</link>
      <description>&lt;PRE&gt;data have;
length ssn $9;
input ssn;
datalines;
000000000
112334588
426-277
;run; &lt;/PRE&gt;
&lt;P&gt;Creates the following output&lt;/P&gt;
&lt;P&gt;ssn&lt;BR /&gt;000000000&lt;BR /&gt;112334588&lt;BR /&gt;426-277&lt;/P&gt;
&lt;P&gt;I need to do the following;&lt;/P&gt;
&lt;P&gt;if the ssn is a blank or if there is any character except consecutive 9 numbers I need it to show as 000000000&amp;nbsp; where this is a character&lt;/P&gt;
&lt;P&gt;Does compress work in this situation?&amp;nbsp; I have not really used compress much.&amp;nbsp; So in the example here I need for 426-277 to read 000426277&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jul 2021 17:39:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compress-and-leading-zeros-if-needed-in-datastep/m-p/755424#M238396</guid>
      <dc:creator>Q1983</dc:creator>
      <dc:date>2021-07-20T17:39:06Z</dc:date>
    </item>
    <item>
      <title>Re: Compress and leading zeros if needed in datastep</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compress-and-leading-zeros-if-needed-in-datastep/m-p/755441#M238398</link>
      <description>&lt;P&gt;Instead of using compress to remove characters, you can use it to keep characters, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ssn=compress(ssn,'0123456789','k');&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;where 'k' says to keep the characters in the 2nd argument.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can then prefix the resulting ssn with 9 zeroes, and take a substr of the last 9 characters:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  length ssn $9;
  input ssn $9.;
datalines;
000000000
112334588
426-277  
            
run; 

data want;
  set have;
  put 'Before: ' ssn= @;
  ssn=compress(ssn,'0123456789','k');
  put @25 'between: ' ssn= @ ;
  ssn=substr(cats('000000000',ssn),lengthn(ssn)+1);  
  put @45 '  After:' ssn  ;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The substr function tells sas to take characters starting at position lengthn(ssn)+1.&amp;nbsp; &amp;nbsp;lengthn will return a 0 for a blank argument, while length will return a 1).&amp;nbsp; &amp;nbsp;I've added a fourth blank SSN to the sample data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This means for ssn='426277' (after the first compress), you would get&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;000000000426277&amp;nbsp; as the first arg of the 2nd compress&lt;/LI&gt;
&lt;LI&gt;7&amp;nbsp; as the 2nd arg of the 2nd compress&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;which means the substr starts in position 7 of 000000000426277, yielding&amp;nbsp; &amp;nbsp;000426277.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Jul 2021 18:07:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compress-and-leading-zeros-if-needed-in-datastep/m-p/755441#M238398</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-07-20T18:07:54Z</dc:date>
    </item>
  </channel>
</rss>

