substring from right, last 5 characters

Reply
Contributor
Posts: 69

substring from right, last 5 characters

I need to evaluate the last 5 characters of a variable.  I have a list of report names (variable lengths) and I need to just check that the last 5 characters are not '_SX0Z'.  How do I do that?  Thanks,

Super User
Posts: 17,885

Re: substring from right, last 5 characters

Reverse and use substring?

if substr(reverse, 1, 5) ne 'Z0XS_'...

Super User
Posts: 9,687

Re: substring from right, last 5 characters

Case sensitive ?

data sample;
  infile datalines truncover;
  input have $256.;
  datalines4;
ABC Lmtd. text 12 td 9595_SX0Z
1st U.S. Comp, something something_SX0Z
United 12 Inc. fhfhfh  55858  k. DDD F
1st Centennial Bancorp operates as the bank holding company
;;;;
run;
data want;
 set sample;
 if not prxmatch('/_SX0Z$/i',trim(have));
run;

Xia Keshan

Super User
Posts: 6,962

Re: substring from right, last 5 characters

Use substr(variable,length(variable)-4,5); make sure that "variable" is at least 5 characters long.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 69

Re: substring from right, last 5 characters

I am reading in the values from a comma delimited csv file, so the values may be upper or lower or a combo of both, not sure if this matters.

Trusted Advisor
Posts: 1,131

Re: substring from right, last 5 characters

Hi ,

i used the sample data of Xia Keshan, and tried the following code , it can identify and flag those records irrespective of there case

data sample;

  infile datalines truncover;

  input have $256.;

  datalines4;

ABC Lmtd. text 12 td 9595_SX0Z

1st U.S. Comp, something something_SX0Z

United 12 Inc. fhfhfh  55858  k. DDD F

1st Centennial Bancorp operates as the bank holding company

;;;;

run;

data want;

set sample;

if index(lowcase(substr(have,length(have)-4,5)),'_sx0z')=0 then flag=1;

run;

Hope it helps.

Thanks,

Jag

Thanks,
Jag
Super User
Posts: 17,885

Re: substring from right, last 5 characters

If case matters add up case to the function:


if upcase(substr(reverse, 1, 5)) ne 'Z0XS_' then do;


end;

Occasional Contributor
Posts: 15

Re: substring from right, last 5 characters

Reeza's answer is a good one.

You might want to reverse it back so that it makes more sense when you're looking at the code.

if reverse(substr(reverse(upcase(reportname)),1,5)) ne '_SX0Z' then do;

end;

Ask a Question
Discussion stats
  • 7 replies
  • 513 views
  • 0 likes
  • 6 in conversation