Extracting word from string.

Accepted Solution Solved
Reply
Contributor
Posts: 28
Accepted Solution

Extracting word from string.

Hi all,

 

I am new SAS user and I am working on a big data. I have one charecter string variable and i need to extarct a specific word from it which is located at different positions of string. For example.

 

If i  want to extract word exercise from these two strings, how can i do that.

 

1- Exercise on bed

2- Person able to exercise.

 

Thanks


Accepted Solutions
Solution
‎12-29-2016 10:07 AM
Contributor
Posts: 52

Re: Extracting word from string.

If i understood correctly, you have a huge file with many records and want to output the records which contain the word - 'Excercise'. you can use the index function to check if the record contains the given word, if yes it will be written to the output dataset.

Hope this works.

 

 

data old;
	input REC $50.;
	datalines;
 1- Exercise ON BED
 2- Person able to exercise
 3- sleep
 4- blah blah blah
 5- Exercise in gym
 6- i am doing Exercise
;
run;

Data new;
	set old;
	IF INDEX(lowcase(REC),'exercise') NE 0 THEN OUTPUT;
run;

 

output:

 

 REC
1- Exercise ON BED 2- Person able to exercise 5- Exercise in gym 6- i am doing Exercise

 


Cheers from India!

Manjeet

View solution in original post


All Replies
Super User
Posts: 17,784

Re: Extracting word from string.

Why extract a word you know?

Look at FINDW or INDEXW to see if word exists and where it is.

Contributor
Posts: 28

Re: Extracting word from string.

Thank you very much Reeza for your help and sorry for late reply beacuse i was on vacation. I was trying to extract a word i know because the data i have is huge and there are many strings that has the word exercise. There are some other words that i need to extract like sitting up, dangling etc. I thought it would be little easier to extract words that i know. I will try the way you are saying. Thanks again.

PROC Star
Posts: 549

Re: Extracting word from string.

I agree with @Reeza, I do not understand why you would extract a word you already know? Smiley Happy

Especially if you alreadyy have the word in a string variable, why dont you just use the string variable? 

Contributor
Posts: 28

Re: Extracting word from string.

Thanks draycut. There are some strings that has the particular words and some of them does not have it. And there are many strings in the dataset thats why i prefer not to use string variable. Thanks again for your reply.

Solution
‎12-29-2016 10:07 AM
Contributor
Posts: 52

Re: Extracting word from string.

If i understood correctly, you have a huge file with many records and want to output the records which contain the word - 'Excercise'. you can use the index function to check if the record contains the given word, if yes it will be written to the output dataset.

Hope this works.

 

 

data old;
	input REC $50.;
	datalines;
 1- Exercise ON BED
 2- Person able to exercise
 3- sleep
 4- blah blah blah
 5- Exercise in gym
 6- i am doing Exercise
;
run;

Data new;
	set old;
	IF INDEX(lowcase(REC),'exercise') NE 0 THEN OUTPUT;
run;

 

output:

 

 REC
1- Exercise ON BED 2- Person able to exercise 5- Exercise in gym 6- i am doing Exercise

 


Cheers from India!

Manjeet
Contributor
Posts: 28

Re: Extracting word from string.

Thanks mnjtrana, you got it right. This is exactly what i was looking for.

Respected Advisor
Posts: 3,887

Re: Extracting word from string.

[ Edited ]

In case "extract" means "remove" from the string then the following code should do.

data have;
	input REC $50.;
	datalines;
Exercise ON BED
 2- Person able to exercise
 3- sleep
 4- blah blah blah
 5- Exercise in gym
 6- i am doing Exercise daily
;
run;

data want;
  set have;
  rec_mod=rec;
  rec_mod=prxchange('s/\bExercise\s*\b//oi',-1,rec_mod);
run;

 

☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 382 views
  • 4 likes
  • 5 in conversation