DATA Step, Macro, Functions and more

Problems with Proc FCMP recursion and tranwrd

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Problems with Proc FCMP recursion and tranwrd

Hi all. 

I'm having a problem with a recursive subroutine in proc FCMP, which I'm using as an example in some training I'm doing.

It is designed to get rid of multiple commas in a string.  The function works fine for me if I copy it into standard data step code, but returns 'gobbldygook' characters when using FCMP and recursion.

The following code illustrates:  Dataset A comes back with the following value in variable teststring: " dÐdÐ,Dave,James,Bob".  I've tried variations on the code, and the first 5 characters always come out as random special characters.

Dataset B comes back with the correct string: "John,Jim,Dave,James,Bob"

I'd really appreciate any help in understanding why it's not working properly. 

Thanks very much,

Robin

options cmplib=work.myfuncs;

proc fcmp outlib = work.myfuncs.mylib;

subroutine decomma(mystring $);

      outargs mystring;

      mystring = tranwrd(mystring,",,",",");

      a = index(mystring,",,");

      if a > 0 then call decomma(mystring);

endsub;

run;

data a;

teststring = "John,,,Jim,,Dave,,,James,Bob";

call decomma(teststring);

run;

data b;

mystring = "John,,,Jim,,Dave,,,James,Bob";

mystring = tranwrd(mystring,",,",",");

mystring = tranwrd(mystring,",,",",");

mystring = tranwrd(mystring,",,",",");

mystring = tranwrd(mystring,",,",",");

mystring = tranwrd(mystring,",,",",");

mystring = tranwrd(mystring,",,",",");

run;


Accepted Solutions
Solution
‎01-11-2013 08:06 AM
Super Contributor
Posts: 345

Re: Problems with Proc FCMP recursion and tranwrd

Tested your programm using SAS 9.2 M3 and 9.3 M2. It seems to be a bug in 9.2, code works without any problem in 9.3.

View solution in original post


All Replies
Solution
‎01-11-2013 08:06 AM
Super Contributor
Posts: 345

Re: Problems with Proc FCMP recursion and tranwrd

Tested your programm using SAS 9.2 M3 and 9.3 M2. It seems to be a bug in 9.2, code works without any problem in 9.3.

Occasional Contributor
Posts: 6

Re: Problems with Proc FCMP recursion and tranwrd

Posted in reply to andreas_lds

Thank you very much for the testing.  So it appears that from a SAS code point of view, there isn't actually a mistake, and it's a bug?

Respected Advisor
Posts: 3,799

Re: Problems with Proc FCMP recursion and tranwrd

Do you need recursion?

data _null_;
   mystring =
"John,,,Jim,,Dave,,,James,Bob";
  
do while(index(mystring,',,'));
      mystring = tranwrd(mystring,',,',',');
      end;
  
put mystring=;
   run;
Occasional Contributor
Posts: 6

Re: Problems with Proc FCMP recursion and tranwrd

Posted in reply to data_null__

Thanks very much for the responses.  You're right that recursion is not needed here - it's an example I'm using in a training exercise to demonstrate the principle of recursion, and the fact it's possible to use it in SAS.  Unfortunately, since it's buggy in my version of SAS, it appears that it's not a very example!

Respected Advisor
Posts: 3,799

Re: Problems with Proc FCMP recursion and tranwrd

Can you make it work with two arguments to your call routine.  Where in and out are different variables.   That might "fix" the problem.

Occasional Contributor
Posts: 6

Re: Problems with Proc FCMP recursion and tranwrd

Posted in reply to data_null__

I'll try that when I'm back at work next week.  I've now been able to test on a different v9.2 system with no problems, and it seems to be a bug with the specific version of SAS I'm using.  Thanks again for replies.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 314 views
  • 1 like
  • 3 in conversation