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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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