Hello,
I am using the following code:
proc fcmp outlib = work.functions.testing;
function getfirstPercentage(agency $, sbwe_agencies $);
pc = 0;
if index(sbwe_agencies,agency)>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;
The function getSecondPercentage calls getFirstPercentage, so I would expect FirstPercentage and SecondPercentage to both return 1. However, I get this
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
Why doesn't SecondPercentage return 1 as well? How do I need to do to get SecondPercentage to return 1? Is there a way to debug a user-written function?
Thanks for any help
Andrew
Try
function getfirstPercentage(agency $, sbwe_agencies $); pc = 0; if index(sbwe_agencies,STRIP(agency))>0 then do; pc = 1; end; return(pc); endsub;
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.
This is analogous to some of the other character operations in SAS.
Here is an example with the || operator and Index functions:
data example; length v $ 10; v='abc'; q= '"'||v||'"'; put q=; z = index('123abc',v); put z=; run;
The log will show:
q="abc "
z=0
because the full length is used in both the concatenation and the Index function.
Try
function getfirstPercentage(agency $, sbwe_agencies $); pc = 0; if index(sbwe_agencies,STRIP(agency))>0 then do; pc = 1; end; return(pc); endsub;
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.
This is analogous to some of the other character operations in SAS.
Here is an example with the || operator and Index functions:
data example; length v $ 10; v='abc'; q= '"'||v||'"'; put q=; z = index('123abc',v); put z=; run;
The log will show:
q="abc "
z=0
because the full length is used in both the concatenation and the Index function.
You are a genius and a life saver. I know about trailing blanks, but they still catch me out.
Thanks you for taking the time to sort this out for me
Andrew
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.