<?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: Calling a user-written function within a user-written function gives wrong result in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calling-a-user-written-function-within-a-user-written-function/m-p/747430#M234590</link>
    <description>&lt;P&gt;Try&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;function getfirstPercentage(agency $, sbwe_agencies $);
pc = 0;
if index(sbwe_agencies,STRIP(agency))&amp;gt;0
	then do;
			pc = 1;
		end;
return(pc);
endsub;&lt;/PRE&gt;
&lt;P&gt;I think what is happening is the way the Second passes the value to first is defaulting to a length that includes trailing blanks and those are used in the second call.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is analogous to some of the other character operations in SAS.&lt;/P&gt;
&lt;P&gt;Here is an example with the || operator and Index functions:&lt;/P&gt;
&lt;PRE&gt;data example;
   length v $ 10;
   v='abc';
   q= '"'||v||'"';
   put q=;
   z = index('123abc',v);
   put z=;
run;&lt;/PRE&gt;
&lt;P&gt;The log will show:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;q="abc       "&lt;BR /&gt;z=0&lt;BR /&gt;
&lt;/PRE&gt;
&lt;P&gt;because the full length is used in both the concatenation and the Index function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 11 Jun 2021 17:34:23 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2021-06-11T17:34:23Z</dc:date>
    <item>
      <title>Calling a user-written function within a user-written function gives wrong result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-a-user-written-function-within-a-user-written-function/m-p/747402#M234575</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am using the following code:&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.testing;
function getfirstPercentage(agency $, sbwe_agencies $);
pc = 0;
if index(sbwe_agencies,agency)&amp;gt;0
	then do;
			pc = 1;
		end;
return(pc);
endsub;

function getSecondPercentage(agency $, sbwe_agencies $);
co = getfirstPercentage(agency, sbwe_agencies);
return(co);
endsub;

quit;

options cmplib=(work.functions);

data test;
	sbwe_agencies = "BSCV904,BSCV905,BSCV908,SBCV135,SBCV136,SBCV137";
	agency = "SBCV136";
	firstPercentage = getfirstPercentage(agency , sbwe_agencies);
	SecondPercentage = getSecondPercentage(agency , sbwe_agencies);
	put firstPercentage= SecondPercentage=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The function getSecondPercentage calls&amp;nbsp;getFirstPercentage, so I would expect&amp;nbsp;FirstPercentage and SecondPercentage to both return 1.&amp;nbsp; However, I get this&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;52         data test;
53         	sbwe_agencies = "BSCV904,BSCV905,BSCV908,SBCV135,SBCV136,SBCV137";
54         	agency = "SBCV136";
55         	firstPercentage = getfirstPercentage(agency , sbwe_agencies);
56         	SecondPercentage = getSecondPercentage(agency , sbwe_agencies);
57         	put firstPercentage= SecondPercentage=;
58         run;

firstPercentage=1 SecondPercentage=0&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Why doesn't SecondPercentage return 1 as well?&amp;nbsp; How do I need to do to get&amp;nbsp;SecondPercentage to return 1?&amp;nbsp; Is there a way to debug a user-written function?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for any help&lt;/P&gt;
&lt;P&gt;Andrew&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jun 2021 16:45:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-a-user-written-function-within-a-user-written-function/m-p/747402#M234575</guid>
      <dc:creator>AndrewJones</dc:creator>
      <dc:date>2021-06-11T16:45:23Z</dc:date>
    </item>
    <item>
      <title>Re: Calling a user-written function within a user-written function gives wrong result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-a-user-written-function-within-a-user-written-function/m-p/747430#M234590</link>
      <description>&lt;P&gt;Try&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;function getfirstPercentage(agency $, sbwe_agencies $);
pc = 0;
if index(sbwe_agencies,STRIP(agency))&amp;gt;0
	then do;
			pc = 1;
		end;
return(pc);
endsub;&lt;/PRE&gt;
&lt;P&gt;I think what is happening is the way the Second passes the value to first is defaulting to a length that includes trailing blanks and those are used in the second call.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is analogous to some of the other character operations in SAS.&lt;/P&gt;
&lt;P&gt;Here is an example with the || operator and Index functions:&lt;/P&gt;
&lt;PRE&gt;data example;
   length v $ 10;
   v='abc';
   q= '"'||v||'"';
   put q=;
   z = index('123abc',v);
   put z=;
run;&lt;/PRE&gt;
&lt;P&gt;The log will show:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;q="abc       "&lt;BR /&gt;z=0&lt;BR /&gt;
&lt;/PRE&gt;
&lt;P&gt;because the full length is used in both the concatenation and the Index function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jun 2021 17:34:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-a-user-written-function-within-a-user-written-function/m-p/747430#M234590</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-06-11T17:34:23Z</dc:date>
    </item>
    <item>
      <title>Re: Calling a user-written function within a user-written function gives wrong result</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calling-a-user-written-function-within-a-user-written-function/m-p/747462#M234599</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You are a genius and a life saver.&amp;nbsp; I know about trailing blanks, but they still catch me out.&lt;/P&gt;
&lt;P&gt;Thanks you for taking the time to sort this out for me&lt;/P&gt;
&lt;P&gt;Andrew&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jun 2021 18:27:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calling-a-user-written-function-within-a-user-written-function/m-p/747462#M234599</guid>
      <dc:creator>AndrewJones</dc:creator>
      <dc:date>2021-06-11T18:27:09Z</dc:date>
    </item>
  </channel>
</rss>

