Help using Base SAS procedures

Substring/Scan from string

Accepted Solution Solved
Reply
Contributor
Posts: 60
Accepted Solution

Substring/Scan from string

Hi to all

My data:

data  have;

input string $40.;

datalines;

33_we#fff23BOB_TS=234623623624756_TOO

BOB_TS=57335623566654756BO

60pBOB_TS=343467364563423TOp_#

WWBOB_TS=2346752665634cc90

9789BOB_TS=9872363566756Alll

ldldBOB_TS=2340235674025634efrwe

;

run;

I want to extract first 10 digits after string 'BOB_TS=' (example:  have 60pBOB_TS=343467563423TOp_#  want 3434675634 )

Ca somebody help me with this?

Thank you in advance

Bob


Accepted Solutions
Solution
‎05-20-2014 09:38 AM
Super Contributor
Posts: 275

Re: Substring/Scan from string


data  have;
input string $40.;
num=substr(string,index(string,'=')+1,10);
datalines;
33_we#fff23BOB_TS=234623623624756_TOO
BOB_TS=57335623566654756BO
60pBOB_TS=343467364563423TOp_#
WWBOB_TS=2346752665634cc90
9789BOB_TS=9872363566756Alll
ldldBOB_TS=2340235674025634efrwe
;
run;

View solution in original post


All Replies
Solution
‎05-20-2014 09:38 AM
Super Contributor
Posts: 275

Re: Substring/Scan from string


data  have;
input string $40.;
num=substr(string,index(string,'=')+1,10);
datalines;
33_we#fff23BOB_TS=234623623624756_TOO
BOB_TS=57335623566654756BO
60pBOB_TS=343467364563423TOp_#
WWBOB_TS=2346752665634cc90
9789BOB_TS=9872363566756Alll
ldldBOB_TS=2340235674025634efrwe
;
run;

Contributor
Posts: 60

Re: Substring/Scan from string

Thank you very much

Working like a charm

Bob

Respected Advisor
Posts: 3,156

Re: Substring/Scan from string

Using PRX,

data  have;

input string $40.;

digits=prxchange('s/(.+)((?<=BOB_TS=)\d{10}?)(.+)/$2/io', 1,string);

datalines;

33_we#fff23BOB_TS=234623623624756_TOO

BOB_TS=57335623566654756BO

60pBOB_TS=343467364563423TOp_#

WWBOB_TS=2346752665634cc90

9789BOB_TS=9872363566756Alll

ldldBOB_TS=2340235674025634efrwe

;

run;

Regards,

Haikuo

Super User
Posts: 10,041

Re: Substring/Scan from string

Backward Match for Peal Regular Expression :

data  have;

input string $40.;

re = prxparse('/((?<=BOB_TS=)\d{10})/io');

if prxmatch(re,string) then want= prxposn(re,1,string);

datalines;

33_we#fff23BOB_TS=234623623624756_TOO

BOB_TS=57335623566654756BO

60pBOB_TS=343467364563423TOp_#

WWBOB_TS=2346752665634cc90

9789BOB_TS=9872363566756Alll

ldldBOB_TS=2340235674025634efrwe

;

run;

Xia Keshan

Trusted Advisor
Posts: 1,228

Re: Substring/Scan from string

data want;

set have;

bob=substr(string,find(string,'BOB_TS=')+7,10);

run;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 266 views
  • 7 likes
  • 5 in conversation