BookmarkSubscribeRSS Feed
Pandu
Fluorite | Level 6

I have a data set with variable name called description.

 

Description="The mobile is produced by Apple"

 

From the above string, i want to extract from by Apple

the length is not consistence but i wanted to extract from by.

 

Thanks in advance.

6 REPLIES 6
Jagadishkatam
Amethyst | Level 16
data want;
input description&$100.;
new=substr(description,index(description,'by'));
cards;
The mobile is produced by Apple
;
Thanks,
Jag
Pandu
Fluorite | Level 6

Hi Jag...I dont want to use by...

after certain string i want to dived into two strings.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Provide test data in the form of a datastep:

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

Then show what you want out at the end from that test data.  Also explain fully what your problem is and logic.  Your original post only stipulated split by "by", we cannot guess any further what your intentions are!

ballardw
Super User

@Pandu wrote:

Hi Jag...I dont want to use by...

after certain string i want to dived into two strings.


Show explicit desired output.

This description of "...I dont want to use by" is not quite consistent with your original question.

Also It might help to show more than one worked example if you need to use something other than "by" as the split element.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

I would recommend using the findw() function rather than index, which will find any occurrences of the word "by" in the string rather than any occurrences (e.g. it index would find it in "byway").

data want;
  input description $100.;
  new=substr(description,findw(description,'by'));
cards;
The mobile is produced by Apple
;
run;
Jagadishkatam
Amethyst | Level 16

alternatively by perl regular expressions

 

data want;
input description&$100.;
new=prxchange('s/(.*)(by*)/$2/i',-1,strip(description));
cards;
The mobile is produced by Apple
;
Thanks,
Jag

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 1268 views
  • 1 like
  • 4 in conversation