Hi,
I am trying to extract the characters before the second "-" in the value (name is pagepath). Any ideas or thoughts are appreciated!
This should work:
want = catx('-', scan(string, 1, '-'), scan(string, 2, '-') );
Thanks for the reply, I used scan function and I could extract characters before the first "-", but I'm not sure how to extract characters before the second "-". Here's an example, abc-def-ghi, I want to get result as "abc-def".
Show your code. What did you put as the second and third parameters?
SCAN(string, count <, character-list <, modifier>>)
@yichentian226 Here's one way to do it:
data have;
input pagepath $25.;
datalines;
abc-def-ghi-jkl
testing-it-again
;
run;
data want (drop=p i);
set have;
p = 0;
do i=1 to 2 until(p=0);
p = find(pagepath, '-', p+1);
end;
if p ne 0 then str_before_2nd_hyphen = substr(pagepath, 1, p-1);
run;
Reference: https://blogs.sas.com/content/sgf/2019/06/26/finding-n-th-instance-of-a-substring-within-a-string/
Thanks it's great! Very helpful
Program:
data Have;
DROP Second_Dash_Position;
DROP String_Length;
LENGTH With_Dashes $35;
INPUT With_Dashes $;
Second_Dash_Position = FIND(With_Dashes, '-', (INDEX(With_Dashes, '-') + 1));
String_Length = LENGTH(With_Dashes) - (LENGTH(With_Dashes) - Second_Dash_Position) - 1;
IF Second_Dash_Position THEN
Before_the_Dashes = SUBSTR(With_Dashes, 1, String_Length);
datalines;
happy-go-lucky
fleet-of-foot
down-in-the-mouth
woe-begone
fine-and-dandy
In-like-Flynn
The-more-the-merrier
;
RUN;
Results:
Jim
Thanks very much it worked!
This should work:
want = catx('-', scan(string, 1, '-'), scan(string, 2, '-') );
I really like your way of solving it! Thanks it's great 🙂
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: