SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
caveman529
Calcite | Level 5

Hi, everyone:

Just wondering how to extract the red portion of the following strings?

client/data/20/0000893220-98-000530.txt

client/data/810084/0001193125-08-059176.txt

client/data/914208/0000950129-07-000703.txt

client/data/1036213/0001036213-98-000015.txt

client/data/1103345/0001103345-05-000031.txt

client/data/1161175/0000950123-10-012321.txt

The red string always fall between the third slash and the .txt portion.

I understand I need to use anypunct and anynum to solve this problem.  However, after several try, I still get 0.  Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data _null_;
input x $100.;
y=scan(x,-2,'./');
put x= y=;
cards;
client/data/20/0000893220-98-000530.txt
client/data/810084/0001193125-08-059176.txt
client/data/914208/0000950129-07-000703.txt
client/data/1036213/0001036213-98-000015.txt
client/data/1103345/0001103345-05-000031.txt
client/data/1161175/0000950123-10-012321.txt
;
run;

Ksharp

View solution in original post

5 REPLIES 5
Ksharp
Super User
data _null_;
input x $100.;
y=scan(x,-2,'./');
put x= y=;
cards;
client/data/20/0000893220-98-000530.txt
client/data/810084/0001193125-08-059176.txt
client/data/914208/0000950129-07-000703.txt
client/data/1036213/0001036213-98-000015.txt
client/data/1103345/0001103345-05-000031.txt
client/data/1161175/0000950123-10-012321.txt
;
run;

Ksharp

caveman529
Calcite | Level 5

Thank you, Ksharp and sunilzood!  It works like charm on my data.  Could you explain a little bit of the more about the

y=scan(x,4,'./');

Especially, the 4 and ./ part, since there are multiple slashes...  Thank you!

Ksharp
Super User

4 means the fourth part.

./ means take both . and / as separator .

caveman529
Calcite | Level 5

Wow, this is concise!

sunilzood
Calcite | Level 5

Ksharp solution is Perfect , But when I tried it skipped alternate rows. A little modification just by adding " : "  will work perfect.  Please try:

data _null_;

input x : $  100.;

y=scan(x,4,'./');

put x= y=;

cards;

client/data/20/0000893220-98-000530.txt

client/data/810084/0001193125-08-059176.txt

client/data/914208/0000950129-07-000703.txt

client/data/1036213/0001036213-98-000015.txt

client/data/1103345/0001103345-05-000031.txt

client/data/1161175/0000950123-10-012321.txt

;

run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1079 views
  • 6 likes
  • 3 in conversation