<?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 Math oddity between data step and proc fcmp in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Math-oddity-between-data-step-and-proc-fcmp/m-p/686810#M208454</link>
    <description>&lt;P&gt;SAS 9.4M5&lt;BR /&gt;EG 7.14 x64&lt;BR /&gt;&lt;BR /&gt;Data Step used for testing and debugging:&lt;/P&gt;&lt;P&gt;data _null_;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;length szName $ 12;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;szCourse = 'PHIL ';&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;szNmbr1 = '112 ';&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;szNmbr2 = '112LAB';&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;intLen = lengthn(cats(szCourse, szNmbr1));&lt;BR /&gt;intInsert = 11 - intLen;&lt;BR /&gt;szName = cat(trim(szCourse), repeat(' ', intInsert), trim(szNmbr1));&lt;BR /&gt;intNameLen = lengthn(szName);&lt;BR /&gt;intNameLen = length(szName);&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;intLen = lengthn(cats(szCourse, szNmbr2));&lt;BR /&gt;intInsert = 11 - intLen;&lt;BR /&gt;szName = cat(trim(szCourse), repeat(' ', intInsert), trim(szNmbr2));&lt;BR /&gt;intNameLen = lengthn(szName);&lt;BR /&gt;intNameLen = length(szName);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The data step works as expected; it concatenates the course and number, separated by a derived number of spaces to produce a left-aligned course name, x spaces, right-aligned course number that is always 12 total character long. This comports with the SAS documentation for the repeat() function as an n+1 result.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I move the core code into a proc FCMP function, repeat() behaves differently for some reason - it produces an n+2 result, requiring me to adjust my math.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC FCMP OUTLIB=&amp;amp;IE_Functions;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;FUNCTION Build_Course_ID(szSubj $, szNumber $) $ 12;&lt;BR /&gt;length szName $ 12;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;intInsert = 10 - lengthn(cats(szSubj, szNumber));&lt;BR /&gt;szName = cat(trim(szSubj), repeat(' ', intInsert), trim(szNumber));&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;return(szName);&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;ENDSUB;&lt;/P&gt;&lt;P&gt;RUN;&lt;/P&gt;</description>
    <pubDate>Fri, 25 Sep 2020 19:54:33 GMT</pubDate>
    <dc:creator>JimHarrison</dc:creator>
    <dc:date>2020-09-25T19:54:33Z</dc:date>
    <item>
      <title>Math oddity between data step and proc fcmp</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Math-oddity-between-data-step-and-proc-fcmp/m-p/686810#M208454</link>
      <description>&lt;P&gt;SAS 9.4M5&lt;BR /&gt;EG 7.14 x64&lt;BR /&gt;&lt;BR /&gt;Data Step used for testing and debugging:&lt;/P&gt;&lt;P&gt;data _null_;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;length szName $ 12;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;szCourse = 'PHIL ';&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;szNmbr1 = '112 ';&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;szNmbr2 = '112LAB';&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;intLen = lengthn(cats(szCourse, szNmbr1));&lt;BR /&gt;intInsert = 11 - intLen;&lt;BR /&gt;szName = cat(trim(szCourse), repeat(' ', intInsert), trim(szNmbr1));&lt;BR /&gt;intNameLen = lengthn(szName);&lt;BR /&gt;intNameLen = length(szName);&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;intLen = lengthn(cats(szCourse, szNmbr2));&lt;BR /&gt;intInsert = 11 - intLen;&lt;BR /&gt;szName = cat(trim(szCourse), repeat(' ', intInsert), trim(szNmbr2));&lt;BR /&gt;intNameLen = lengthn(szName);&lt;BR /&gt;intNameLen = length(szName);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The data step works as expected; it concatenates the course and number, separated by a derived number of spaces to produce a left-aligned course name, x spaces, right-aligned course number that is always 12 total character long. This comports with the SAS documentation for the repeat() function as an n+1 result.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I move the core code into a proc FCMP function, repeat() behaves differently for some reason - it produces an n+2 result, requiring me to adjust my math.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC FCMP OUTLIB=&amp;amp;IE_Functions;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;FUNCTION Build_Course_ID(szSubj $, szNumber $) $ 12;&lt;BR /&gt;length szName $ 12;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;intInsert = 10 - lengthn(cats(szSubj, szNumber));&lt;BR /&gt;szName = cat(trim(szSubj), repeat(' ', intInsert), trim(szNumber));&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;return(szName);&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;ENDSUB;&lt;/P&gt;&lt;P&gt;RUN;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Sep 2020 19:54:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Math-oddity-between-data-step-and-proc-fcmp/m-p/686810#M208454</guid>
      <dc:creator>JimHarrison</dc:creator>
      <dc:date>2020-09-25T19:54:33Z</dc:date>
    </item>
    <item>
      <title>Re: Math oddity between data step and proc fcmp</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Math-oddity-between-data-step-and-proc-fcmp/m-p/686904#M208488</link>
      <description>&lt;P&gt;Works for me.&lt;BR /&gt;Careful how you manage the trailing spaces.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc fcmp outlib=WORK.FUNCS.GEN;
  function Build_Course_ID(STR1 $, STR2 $) $12 ;
    LEN = 12 - length(cats(STR1, STR2));
    OUT = catx(repeat(' ', LEN-1), STR1, STR2);
    return( OUT );
  endsub;
run;
                                    
options cmplib=WORK.FUNCS;

data t; 
  length A $12;
  A=Build_Course_ID('xxxxx','yyyyy');
  put A=;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;A=xxxxx  yyyyy
&lt;/PRE&gt;</description>
      <pubDate>Sat, 26 Sep 2020 09:39:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Math-oddity-between-data-step-and-proc-fcmp/m-p/686904#M208488</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2020-09-26T09:39:22Z</dc:date>
    </item>
  </channel>
</rss>

