BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
RobinL
Calcite | Level 5

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;

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

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

6 REPLIES 6
andreas_lds
Jade | Level 19

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.

RobinL
Calcite | Level 5

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?

data_null__
Jade | Level 19

Do you need recursion?

data _null_;
   mystring =
"John,,,Jim,,Dave,,,James,Bob";
  
do while(index(mystring,',,'));
      mystring = tranwrd(mystring,',,',',');
      end;
  
put mystring=;
   run;
RobinL
Calcite | Level 5

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!

data_null__
Jade | Level 19

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

RobinL
Calcite | Level 5

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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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