<?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: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310517#M66999</link>
    <description>&lt;P&gt;You created a FORMAT, but then you tried to use an INFORMAT that you did NOT create. Try running this code instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data x;
  input x $6 ;
  put x= $leadZ. ;
datalines;
test
tes
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also you forgot to include a $ in your function defintion so that SAS knows that it returns a character string.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc fcmp outlib=work.functions.myfunc;
function leadZ(x $) $;
xx = cats(repeat('0',6-length(x)-1),x);
return (xx);
endsub;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 09 Nov 2016 20:55:05 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2016-11-09T20:55:05Z</dc:date>
    <item>
      <title>[SAS Formats] Is it possible to create a format to add leading zeros in a character variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310451#M66958</link>
      <description>&lt;P&gt;Hi experts!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been searching but I did not found anything about creating a sas format to add leading zeros in a character variable using PROC FORMAT.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The objective is to use this format whenever I want, for example, using it in SAS Marketing Automation export definitions etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried links similar to this: &lt;A href="http://www2.sas.com/proceedings/sugi31/243-31.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi31/243-31.pdf&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone know?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;BS&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2017 12:16:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310451#M66958</guid>
      <dc:creator>bsas94</dc:creator>
      <dc:date>2017-05-02T12:16:06Z</dc:date>
    </item>
    <item>
      <title>Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310453#M66960</link>
      <description>&lt;P&gt;Why do you wan to solve this with a format? &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Could you not just concatenate woth a leading zero?&lt;/P&gt;</description>
      <pubDate>Wed, 09 Nov 2016 17:15:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310453#M66960</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2016-11-09T17:15:47Z</dc:date>
    </item>
    <item>
      <title>Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310455#M66961</link>
      <description>&lt;P&gt;Or like this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    length CharacterVar $ 5 WithZeros $ 5;
    input CharacterVar;

    WithZeros = put(input(CharacterVar,best5.),z5.);
datalines;
73345
6378
286
26
4
9644
94
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Nov 2016 17:20:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310455#M66961</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2016-11-09T17:20:27Z</dc:date>
    </item>
    <item>
      <title>Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310457#M66963</link>
      <description>&lt;P&gt;I don't what the range of your char var is. this will work for 2 bytes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input id var1 $2.;&lt;BR /&gt;datalines;&lt;BR /&gt;1 8&lt;BR /&gt;2 9&lt;BR /&gt;3 10&lt;/P&gt;
&lt;P&gt;4 99&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;proc print;&lt;BR /&gt;run;&lt;BR /&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;newvar=put(input(var1,3.),z3.);&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Nov 2016 17:28:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310457#M66963</guid>
      <dc:creator>GreggB</dc:creator>
      <dc:date>2016-11-09T17:28:59Z</dc:date>
    </item>
    <item>
      <title>Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310458#M66964</link>
      <description>&lt;P&gt;&lt;SPAN&gt;The objetive is to use this format whenever I want, for example, using it in SAS Marketing Automation export definitions etc. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;It would be necessary to create it using PROC FORMAT so I can save the format to the catalog.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Nov 2016 17:31:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310458#M66964</guid>
      <dc:creator>bsas94</dc:creator>
      <dc:date>2016-11-09T17:31:15Z</dc:date>
    </item>
    <item>
      <title>Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310461#M66965</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Guys,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The objetive is to use this format whenever I want, for example, using it in SAS Marketing Automation export definitions etc.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I should create it using PROC FORMAT.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Tks,&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Nov 2016 17:33:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310461#M66965</guid>
      <dc:creator>bsas94</dc:creator>
      <dc:date>2016-11-09T17:33:46Z</dc:date>
    </item>
    <item>
      <title>Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310466#M66968</link>
      <description>&lt;P&gt;You can create a function via PROC FCMP that does the conversion and implement this via a FORMAT.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The SAS documentation for PROC FORMAT has an example of this type of implementation but converting Celsius to Fahrenheit I believe.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So the answer to your question is, yes, it's possible.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Nov 2016 18:03:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310466#M66968</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-11-09T18:03:09Z</dc:date>
    </item>
    <item>
      <title>Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310470#M66972</link>
      <description>&lt;P&gt;And since a character can't always be read as a number I suggest using this function instead:&lt;/P&gt;
&lt;P&gt;Assuming length of field required is 6.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;xx = cats(repeat('0',6-length(x)-1), x);&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.listendata.com/2016/07/sas-add-leading-zeros-to-variable.html" target="_blank"&gt;http://www.listendata.com/2016/07/sas-add-leading-zeros-to-variable.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You do need&amp;nbsp;SAS 9.3+ for this to work.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Nov 2016 18:12:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310470#M66972</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-11-09T18:12:23Z</dc:date>
    </item>
    <item>
      <title>Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310487#M66986</link>
      <description>&lt;P&gt;Hi Reeza,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm having some troubles. When I run the following program I created:&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;PRE&gt;&lt;CODE class=" language-sas"&gt;proc fcmp outlib=work.functions.myfunc;
function leadZ(x $);
xx = cats(repeat('0',6-length(x)-1),x);
return (xx);
endsub;
run;

options cmplib=work.functions;

proc format;
value $leadZ other=[leadZ()];
run;

data x;
infile datalines;
input x $leadZ.;
datalines;
test
tes
;
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;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I get the following error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;2                                                          The SAS System                          13:38 Wednesday, November 9, 2016

36         
37         data x;
38         infile datalines;
39         input x $leadZ.;
                   _______
                   485
NOTE 485-185: Informat $LEADZ was not found or could not be loaded.&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you have any idea how to fix it?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PS: It's my first time creating a function &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Tks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Nov 2016 18:58:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310487#M66986</guid>
      <dc:creator>bsas94</dc:creator>
      <dc:date>2016-11-09T18:58:16Z</dc:date>
    </item>
    <item>
      <title>Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310489#M66988</link>
      <description>&lt;P&gt;In proc format you create a format not an informat.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Either apply the format instead of using it as an informat or create an informat.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Im assuming you also tried FILL with a picture format.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Nov 2016 19:00:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310489#M66988</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-11-09T19:00:29Z</dc:date>
    </item>
    <item>
      <title>Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310495#M66992</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You're correct. I'm trying to fill with a picture format.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc fcmp outlib=work.functions.myfunc;
	function leadZ(x $);
		xx = cats(repeat('0',6-length(x)-1),x);
		return (xx);
	endsub;
run;

options cmplib=work.functions;

proc sql;
	create table x (
		a VARCHAR(6),
		b VARCHAR(6)
		);
	insert into x values("123","123");
quit;

proc print data=x;
format a $leadZ.;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now I don't get any errors, but when I check in the result generated by proc print, there's no difference when it comes to the format being applied.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Tks for the help so far!&lt;/P&gt;</description>
      <pubDate>Wed, 09 Nov 2016 19:22:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310495#M66992</guid>
      <dc:creator>bsas94</dc:creator>
      <dc:date>2016-11-09T19:22:20Z</dc:date>
    </item>
    <item>
      <title>Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310517#M66999</link>
      <description>&lt;P&gt;You created a FORMAT, but then you tried to use an INFORMAT that you did NOT create. Try running this code instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data x;
  input x $6 ;
  put x= $leadZ. ;
datalines;
test
tes
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also you forgot to include a $ in your function defintion so that SAS knows that it returns a character string.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc fcmp outlib=work.functions.myfunc;
function leadZ(x $) $;
xx = cats(repeat('0',6-length(x)-1),x);
return (xx);
endsub;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Nov 2016 20:55:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310517#M66999</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2016-11-09T20:55:05Z</dc:date>
    </item>
    <item>
      <title>Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310525#M67001</link>
      <description>&lt;P&gt;Full solution put together:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Define function&lt;/P&gt;
&lt;P&gt;2. Create format&lt;/P&gt;
&lt;P&gt;3. Apply format using format statement&lt;/P&gt;
&lt;P&gt;4. Convert to new variable using PUT() statement&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*1*/
proc fcmp outlib=work.functions.myfunc;
function leadZ(x $) $;
xx = catt(repeat('0',6-length(x)),x);
return (xx);
endsub;
run;

/*2*/
options cmplib=work.functions;

proc format;
value $leadZ other=[leadZ()];
run;

data sample;
  input x $6. ;
  /*3 - for demonstration purpose*/
  y=x; 
  format y $leadz.; 

  z=put(x, $leadz.); /*4*/
datalines;
test
tes
;
run;
proc print data=sample noobs label;
title 'Sample output';
label x='Raw data (x)' 
y='Underlying data is raw data, format applied (y)' 
z='Converted to new char variable (z)';
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Nov 2016 21:15:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310525#M67001</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-11-09T21:15:12Z</dc:date>
    </item>
    <item>
      <title>Re: [SAS Formats] Is it possible to create a format to add leading zeros in a character variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310623#M67016</link>
      <description>&lt;P&gt;It worked just fine!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'll try to find a way to change the part when I explicitly define the variable length is 6.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In any case, thanks a lot!&lt;/P&gt;</description>
      <pubDate>Thu, 10 Nov 2016 11:45:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Formats-Is-it-possible-to-create-a-format-to-add-leading/m-p/310623#M67016</guid>
      <dc:creator>bsas94</dc:creator>
      <dc:date>2016-11-10T11:45:27Z</dc:date>
    </item>
  </channel>
</rss>

