11-21-2017 06:24 PM - edited 12-20-2017 04:50 PM
I want to find the position of the last "-" in the string below
so that last "-" is in position 9.
Can anyone please help?
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;
11-21-2017 06:31 PM
Here's one approach:
11-21-2017 07:29 PM
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.
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;
11-21-2017 07:36 PM
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.
11-21-2017 07:38 PM
Need further help from the community? Please ask a new question.