Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Substr problem 1

Status
Not open for further replies.

mikeymay

Programmer
May 2, 2007
10
GB
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
 
check the actual length of region1, if it is 7 or less then you will get an error.

try using a condition to check the length before the conditional statement

RegLen = length(Region1);

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

Your exact example code wont work as it's missing do/end structures. If you want to do it in a one liner you can use the ifc function.

I agree that the most likely problem of the OP is region being too short, that's why example data is really important.

Code:
if length(Region1)>7 then Region2=ifc(substr(Region1,RegLen-7,8)="Division",cat(Region1," Support"),'');
 
Can you include the error message and what the data was when the error was generated? There should be a mini-dump in the log showing the values of the variables when the error occured.
An alternative methods, rather than using substr is to use SCAN.
First off, this works:-
Code:
data test;

  region1 = 'Wine Division';
  RegLen = length(Region1);

  if substr(Region1,RegLen-7,8) = "Division" then
      Region2 = cat(Region1," Support");
run;
so I'm not sure what your error was.
However, this will also work...
Code:
data test;

  region1 = 'Wine Division';

  if scan(Region1,-1,' ') = "Division" then
      Region2 = cat(Region1," Support");
run;
And might get around your length issues...

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
kdt82,

FYI - the code i posted works perfectly in SAS. there is no do/end required for daisy chaining if then if then statements like that.

 
Sastronaut,

My apologies, you are right. I don't believe I have seen this before. It even works for the else statement :eek:

Curiously, where did you find out about this?
Code:
data test;
region1 ='1Divishion';
RegLen = length(Region1);
if reglen > 7 then if substr(Region1,RegLen-7,8) = "Division" then
Region2 = cat(Region1," Support"); else region2 = 'Other'; 
run;
proc print;run;

output;

Code:
                       Reg
                                 Obs     region1      Len    Region2

                                  1     1Divishion     10     Other


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top