BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Barite | Level 11

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Cramming it all into one statement ....

 

data want;

set have;

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

run;

 

 

 

View solution in original post

6 REPLIES 6
AllanBowe
Barite | Level 11

Here's one approach:

 

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

/Allan
SAS Challenges - SASensei
MacroCore library for app developers
SAS networking events (BeLux, Germany, UK&I)

Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs
hhchenfx
Barite | Level 11

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;
Reeza
Super User

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. 

Reeza
Super User
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.
Astounding
PROC Star

Cramming it all into one statement ....

 

data want;

set have;

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

run;

 

 

 

Ksharp
Super User
data _null_;
  string='123-56-8-101212';
  pos=findc(string,'-','b');
  put pos=;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 25926 views
  • 5 likes
  • 5 in conversation