<?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: true length instead of defined length in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/true-length-instead-of-defined-length/m-p/583631#M166157</link>
    <description>You can't afford to ignore messages in the log.&lt;BR /&gt;&lt;BR /&gt;The LENGTH function applies to character strings only, not to numbers.  So that forces SAS to make a numeric to character conversion and it uses a 12 character format.&lt;BR /&gt;&lt;BR /&gt;Try replacing length(prod) with length(left(prod) )  The conversion still takes place, but LENGTH should get the right value.</description>
    <pubDate>Sat, 24 Aug 2019 02:54:16 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2019-08-24T02:54:16Z</dc:date>
    <item>
      <title>true length instead of defined length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/true-length-instead-of-defined-length/m-p/583630#M166156</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to perform an operation where if a certain variable has a missing value, it ends up getting the value of some other variable. But in addition to that, the new value created should be filled up with leading zeros to make the size of the missing variable a certain length.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For eg., the value of my variable id (char 19.) is null, so it needs to get the value of its corresponding 'prod'(num 12.) which in this case is 123. so my id value should be 0000000000000000123.&lt;/P&gt;&lt;P&gt;To do this, I'm using this logic:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if id = null then
id=cats(repeat('0',19-length(prod)-1), prod);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;However, since the output I'm getting is&amp;nbsp;0000000123 instead of&amp;nbsp;0000000000000000123 because the defined length of the 'prod' var is 12. which is set based on the max size of 'prod'. So the length function comes out to be 12 instead of the true length of 'prod' which could be anything upto 12. How do I make my code to see the true length of each prod value rather than the defined length?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Or if there is a better way to perform this operation, I'd appreciate learning about it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 24 Aug 2019 01:55:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/true-length-instead-of-defined-length/m-p/583630#M166156</guid>
      <dc:creator>AJ_Brien</dc:creator>
      <dc:date>2019-08-24T01:55:33Z</dc:date>
    </item>
    <item>
      <title>Re: true length instead of defined length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/true-length-instead-of-defined-length/m-p/583631#M166157</link>
      <description>You can't afford to ignore messages in the log.&lt;BR /&gt;&lt;BR /&gt;The LENGTH function applies to character strings only, not to numbers.  So that forces SAS to make a numeric to character conversion and it uses a 12 character format.&lt;BR /&gt;&lt;BR /&gt;Try replacing length(prod) with length(left(prod) )  The conversion still takes place, but LENGTH should get the right value.</description>
      <pubDate>Sat, 24 Aug 2019 02:54:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/true-length-instead-of-defined-length/m-p/583631#M166157</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-08-24T02:54:16Z</dc:date>
    </item>
    <item>
      <title>Re: true length instead of defined length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/true-length-instead-of-defined-length/m-p/583632#M166158</link>
      <description>That makes sense, thank you!</description>
      <pubDate>Sat, 24 Aug 2019 03:08:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/true-length-instead-of-defined-length/m-p/583632#M166158</guid>
      <dc:creator>AJ_Brien</dc:creator>
      <dc:date>2019-08-24T03:08:34Z</dc:date>
    </item>
    <item>
      <title>Re: true length instead of defined length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/true-length-instead-of-defined-length/m-p/583640#M166160</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265086"&gt;@AJ_Brien&lt;/a&gt;,&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265086"&gt;@AJ_Brien&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Or if there is a better way to perform this operation, I'd appreciate learning about it.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The standard way of writing numeric values with leading zeros is to use the&amp;nbsp;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=leforinforref&amp;amp;docsetTarget=p09lpr3kmbh8fen1qepuv6zc1ldd.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;Z&lt;EM&gt;w.d&lt;/EM&gt; format&lt;/A&gt;&amp;nbsp;(see example below). An explicit numeric-to-character conversion using the &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=n0mlfb88dkhbmun1x08qbh5xbs7e.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;PUT function&lt;/A&gt; avoids the annoying "&lt;FONT face="courier new,courier"&gt;NOTE: Numeric values have been converted to character values&lt;/FONT&gt; ..." in the log.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, the IF condition "&lt;FONT face="courier new,courier"&gt;id = null&lt;/FONT&gt;" will produce a log message "&lt;FONT face="courier new,courier"&gt;NOTE: Variable null is uninitialized.&lt;/FONT&gt;" and a new variable (&lt;FONT face="courier new,courier"&gt;null&lt;/FONT&gt;), unless&amp;nbsp;&lt;FONT face="courier new,courier"&gt;null&lt;/FONT&gt; is an existing variable. To check if character variable&amp;nbsp;&lt;FONT face="courier new,courier"&gt;id&lt;/FONT&gt; is &lt;EM&gt;missing&lt;/EM&gt;, you can use one of several functions (including &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=p06ybg84o0asa4n17l9ieauk58hb.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;MISSING&lt;/A&gt;, &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=p1tth4ltf640din1ey86ubo2lky2.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;CMISS&lt;/A&gt; and &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=p0z9e0q6e8snidn1t9m5pysmkl79.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;LENGTHN&lt;/A&gt;) or compare &lt;FONT face="courier new,courier"&gt;id&lt;/FONT&gt; to a blank character constant:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if id=' ' then id=put(prod, z19.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 24 Aug 2019 08:32:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/true-length-instead-of-defined-length/m-p/583640#M166160</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-08-24T08:32:13Z</dc:date>
    </item>
    <item>
      <title>Re: true length instead of defined length</title>
      <link>https://communities.sas.com/t5/SAS-Programming/true-length-instead-of-defined-length/m-p/583679#M166169</link>
      <description>&lt;P&gt;It seems you are working very hard to do something you don't need to do.&amp;nbsp; To produce a string with leading zeros from a number use the Z format.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if missing(id) then id=put(prod,z19.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or use the COALESCEC() function and eliminate the IF/THEN.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;id=coalescec(id,put(prod,z19.));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 24 Aug 2019 15:47:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/true-length-instead-of-defined-length/m-p/583679#M166169</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-08-24T15:47:55Z</dc:date>
    </item>
  </channel>
</rss>

