BookmarkSubscribeRSS Feed
SASSLICK001
Obsidian | Level 7

Hello All

What is the effective way to extract two words from the following string!? ( I need to extract Day 1)

I can use substr(string,5,5), but sometimes it can be Day 100 or W1 Day 25 (Study 2) in which i need to extract Day 25

I can also use scan function and extract 2 and  3 words and concatenate ! Is there any function which is more robustic?

W10 Day 1 (Study 1)

W1 Day 100 (Study x)

W Day 25

Thanks for all your insights!

J

5 REPLIES 5
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10

SCAN works well, either supplying a positive-value argument (from start-location) or negative-value (from end-location) -- will depend on the result/requirement, however there are likely multiple SAS functions that can accomplish the task.  Similarly, various SAS concatenation functions as well will apply, such as CATX, or using TRIM, possibly even COMPRESS, depending on the rqmt.

Scott Barry
SBBWorks, Inc.

Ksharp
Super User
data have;
input x $40.;
cards;
W10 Day 1 (Study 1)
W1 Day 100 (Study x)
W Day 25
;
run;
data want;
 set have;
if prxmatch('/\s+Day\s+\d+\s+/i',x) then 
want=prxchange('s/.*\s+(Day\s+\d+)\s+.*/$1/i',-1,x);
run;

Xia Keshan

Ksharp
Super User

data want;

set have;

retain pid;

if _n_ eq 1 then pid=prxparse('/\s+Day\s+\d+\s+/i');

call prxsubstr(pid, x, position, length);

if position ne 0 then want = substr(x, position, length);

drop pid position length;

run;

SASSLICK001
Obsidian | Level 7

Thanks Keshan for your reply,

whan about if we have W1 Screening

Ksharp
Super User

It will return a missing value .

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
  • 5 replies
  • 2114 views
  • 0 likes
  • 3 in conversation