<?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 Add leading zeros to numeric variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Add-leading-zeros-to-numeric-variable/m-p/489736#M127948</link>
    <description>&lt;P&gt;Hello guys,&lt;/P&gt;&lt;P&gt;This is a bit tricky. I am trying to add leading zeros to a variable (numvar) depending on the length of charVar variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, in the data below, the first row is charVar=1000 so its length is 4, hence I want to add just one zero before numVar.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried put(numvar, z4.) approach but this wont work for the second row. I also tried CALL SYMPUT and then symget but it is not working either.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any idea on how this can be accomplished will be highly appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;charVar&lt;/TD&gt;&lt;TD&gt;numvar&lt;/TD&gt;&lt;TD&gt;Length_charVar&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;wantVarLeadingzeros&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1000&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;0200'&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10000&lt;/TD&gt;&lt;TD&gt;500&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;00500'&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 24 Aug 2018 19:28:22 GMT</pubDate>
    <dc:creator>rosegarden81</dc:creator>
    <dc:date>2018-08-24T19:28:22Z</dc:date>
    <item>
      <title>Add leading zeros to numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-leading-zeros-to-numeric-variable/m-p/489736#M127948</link>
      <description>&lt;P&gt;Hello guys,&lt;/P&gt;&lt;P&gt;This is a bit tricky. I am trying to add leading zeros to a variable (numvar) depending on the length of charVar variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, in the data below, the first row is charVar=1000 so its length is 4, hence I want to add just one zero before numVar.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried put(numvar, z4.) approach but this wont work for the second row. I also tried CALL SYMPUT and then symget but it is not working either.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any idea on how this can be accomplished will be highly appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;charVar&lt;/TD&gt;&lt;TD&gt;numvar&lt;/TD&gt;&lt;TD&gt;Length_charVar&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;wantVarLeadingzeros&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1000&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;0200'&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;10000&lt;/TD&gt;&lt;TD&gt;500&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;00500'&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Aug 2018 19:28:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-leading-zeros-to-numeric-variable/m-p/489736#M127948</guid>
      <dc:creator>rosegarden81</dc:creator>
      <dc:date>2018-08-24T19:28:22Z</dc:date>
    </item>
    <item>
      <title>Re: Add leading zeros to numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-leading-zeros-to-numeric-variable/m-p/489741#M127949</link>
      <description>&lt;P&gt;PUTC/PUTN will take a character variable as the format. PUT will not.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
y=lengthn(put(rvar, 8. -l));
formatc=catt('Z', y);
want = putn(numvar, formatc);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/228108"&gt;@rosegarden81&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello guys,&lt;/P&gt;
&lt;P&gt;This is a bit tricky. I am trying to add leading zeros to a variable (numvar) depending on the length of charVar variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, in the data below, the first row is charVar=1000 so its length is 4, hence I want to add just one zero before numVar.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried put(numvar, z4.) approach but this wont work for the second row. I also tried CALL SYMPUT and then symget but it is not working either.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any idea on how this can be accomplished will be highly appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD&gt;charVar&lt;/TD&gt;
&lt;TD&gt;numvar&lt;/TD&gt;
&lt;TD&gt;Length_charVar&lt;/TD&gt;
&lt;TD&gt;&lt;STRONG&gt;wantVarLeadingzeros&lt;/STRONG&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1000&lt;/TD&gt;
&lt;TD&gt;200&lt;/TD&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;0200'&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;10000&lt;/TD&gt;
&lt;TD&gt;500&lt;/TD&gt;
&lt;TD&gt;5&lt;/TD&gt;
&lt;TD&gt;00500'&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Aug 2018 19:39:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-leading-zeros-to-numeric-variable/m-p/489741#M127949</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-08-24T19:39:32Z</dc:date>
    </item>
    <item>
      <title>Re: Add leading zeros to numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-leading-zeros-to-numeric-variable/m-p/489746#M127951</link>
      <description>&lt;P&gt;just for fun:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input charVar $	numvar	Length_charVar 	;
cards;
1000	200	4	0200'
10000	500	5	00500'
;

data want;
set have;
_iorc_=Length_charVar-length(put(numvar,8. -l));
want=cats(repeat('0',_iorc_-1),numvar);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Aug 2018 20:10:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-leading-zeros-to-numeric-variable/m-p/489746#M127951</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-08-24T20:10:11Z</dc:date>
    </item>
    <item>
      <title>Re: Add leading zeros to numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-leading-zeros-to-numeric-variable/m-p/489787#M127960</link>
      <description>&lt;P&gt;Use PUTN() function since it takes an expression for the format.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input charVar :$20. numvar Length_charVar wantVarLeadingzeros :$20.;
cards;
1000 200 4 0200
10000 500 5 00500
;

data want ;
  set have ;
  length VarLeadingzeros $20 ;
  VarLeadingzeros=putn(numvar,cats('z',Length_charVar,'.'));
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;       char               Length_      wantVar           Var
Obs     Var     numvar    charVar    Leadingzeros    Leadingzeros

 1     1000       200        4          0200            0200
 2     10000      500        5          00500           00500&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Aug 2018 23:23:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-leading-zeros-to-numeric-variable/m-p/489787#M127960</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-08-24T23:23:31Z</dc:date>
    </item>
    <item>
      <title>Re: Add leading zeros to numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-leading-zeros-to-numeric-variable/m-p/489966#M128062</link>
      <description>Thank you guys for all your responses!...I did try each of your code and it worked!....</description>
      <pubDate>Mon, 27 Aug 2018 04:11:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-leading-zeros-to-numeric-variable/m-p/489966#M128062</guid>
      <dc:creator>rosegarden81</dc:creator>
      <dc:date>2018-08-27T04:11:25Z</dc:date>
    </item>
  </channel>
</rss>

