DATA Step, Macro, Functions and more

Find position of the last character in a string

Accepted Solution Solved
Reply
Super Contributor
Posts: 503
Accepted Solution

Find position of the last character in a string

[ Edited ]

Hi Everyone,

 

I want to find the position of the last "-" in the string below

123-56-8-101212

 

so that last "-" is in position 9.

 

Can anyone please help?

 

Thank you,

 

HC

 

KEY IS: reverse vs regular.

pos=index(reverse(lcat),'-'); give the position of the last '-' using the length of the whole VARIABLE (specified in datastep, format...)

pos1=findc(Lcat,'-','b');give the position of the last '-' using the length of the whole STRING

 


DATA test;
  INFILE DATALINES DELIMITER=',' DSD;
  INPUT status & $100.;
  DATALINES;
"No aproved by Tong"
"No Approval by Paul"
"No Approval by Ryan"
"No approval by Amy"
;
RUN;


data test1; set test;
length=length(Status);
findc=findw(status,'by');
supervisor=substr(status,findc+2);run;

 


Accepted Solutions
Solution
‎11-21-2017 11:29 PM
Super User
Posts: 6,542

Re: Find position of the last character in a string

Cramming it all into one statement ....

 

data want;

set have;

last_dash = length(string) - length(scan(string, -1, '-') );

run;

 

 

 

View solution in original post


All Replies
Contributor
Posts: 53

Re: Find position of the last character in a string

Here's one approach:

 

data _null_;
  string='123-56-8-101212';
  pos=length(string)-index(reverse(string),'-')+1;
  put pos=;
run;

/Allan
Think you know SAS? Take the sasensei challenge..
Super Contributor
Posts: 503

Re: Find position of the last character in a string

Posted in reply to AllanBowe

I think this example work only when there the length of string is fix.

In a datafile, the Length(project) change while the index(reverse(project),'-') count from the end of the whole variable itself, so it is not working.

I need to change to

pos=100-index(reverse(project),'-')+1; 100 is the length of the variable.

 

I wonder if there is any better way.

 

Thank you,

 

HC

 

 

DATA test;
  INFILE DATALINES DELIMITER=',' DSD;
  INPUT project & $100.;
  DATALINES;
"General DYn - CKXy:GD-CQASUB56209-2A10-520"
"General Dyn - Technology:XP-CQA6521-5A10-520"
;
RUN;

data test;
set test;
length=length(project);
last=index(reverse(project),'-');
pos=length(project)-index(reverse(project),'-')+1;
  var=(substr(project,pos,5));
run;
Super User
Posts: 22,857

Re: Find position of the last character in a string

Trim it after reversing it. Otherwise you add leading blanks. 

 

SCAN() allows you to use negative indexes, ie searching backwards which would be a much simpler approach and a single function. 

Super User
Posts: 22,857

Re: Find position of the last character in a string

If you’re not already aware, DLM in the INFILE statement also allows for multiple delimiters if you want to read it into separate variables directly.
Solution
‎11-21-2017 11:29 PM
Super User
Posts: 6,542

Re: Find position of the last character in a string

Cramming it all into one statement ....

 

data want;

set have;

last_dash = length(string) - length(scan(string, -1, '-') );

run;

 

 

 

Super User
Posts: 10,618

Re: Find position of the last character in a string

data _null_;
  string='123-56-8-101212';
  pos=findc(string,'-','b');
  put pos=;
run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 498 views
  • 2 likes
  • 5 in conversation