Help using Base SAS procedures

Problem with Substr function

Reply
N/A
Posts: 0

Problem with Substr function

I am trying to use the following code where Region1 is a string variable


RegLen = length(Region1);

if substr(Region1,RegLen-7,8) = "Division" then
Region2 = cat(Region1," Support");

For some reason this won't work and the error occures at 'RegLen-7' in the substr function. when I replace this with a number, 14 for example' it works, but as Region1 can be any number of characters long I need this calculation in there.


Thanks
N/A
Posts: 0

Re: Problem with Substr function

It works ok here:

data test ;
Region1='TEST Division' ;
RegLen = length(Region1) ;

if substr(Region1,RegLen-7,8) = "Division" then
Region2 = cat(Region1," Support") ;
run ;


Contents of dataset test:

Region1 RegLen Region2
TEST Division 13 Test Division Support
N/A
Posts: 0

Re: Problem with Substr function

Worked out what the problem is now.

Some of the values for Region1 are less than 7 characters long and this is where was falling down.

Simple error, bit took hours to work out!!!!
Ask a Question
Discussion stats
  • 2 replies
  • 105 views
  • 0 likes
  • 1 in conversation