DATA Step, Macro, Functions and more

Extracting word

Accepted Solution Solved
Reply
Frequent Contributor
Frequent Contributor
Posts: 76
Accepted Solution

Extracting word

 

Dear all, 

 

I would like to extract the part between end of  first word and before "vs" .   How I can do it ?

 

For example, I would like to get "Black or African American" this part out.

 

Thank you.

 

Capture.PNG

 

 

 

 


Accepted Solutions
Solution
‎02-22-2017 02:41 PM
PROC Star
Posts: 7,364

Re: Extracting word

data want;
  set have;;
  CALL SCAN(string, 2, start, length);
  want = substr(string, start, index(string, 'vs')-start);
run;

Art, CEO, AnalystFinder.com

 

View solution in original post


All Replies
Frequent Contributor
Posts: 75

Re: Extracting word

[ Edited ]

Is this what you need?

 

WantString = substr(HaveString,1,indexw(string,'vs')-1);

 

Edited to include data step code to show how my statement works:

data Have;
length HaveString $100.;
HaveString='Black or African American vs White'; output;
HaveString='Hispanic or Latino vs White';output;
HaveString='Other Race        vs White';output;
;
run;

data Want;
set Have;
WantString = substr(HaveString,1,indexw(HaveString,'vs')-1);
run;

 

Editing again as I noticed that words such as 'Race' appear to be in the same column as the value for Race. In this case, this code will work:

data Have;
length HaveString $100.;
HaveString='Race   Black or African American vs White'; output;
HaveString='Race   Hispanic or Latino vs White';output;
HaveString='Race        Other Race        vs White';output;
;
run;

data Want;
set Have;
WantString = substr(HaveString, indexw(HaveString,scan(HaveString,2)), indexw(HaveString,'vs')-length(substr(HaveString,1,indexw(HaveString,scan(HaveString,2)))));
run;

 

 

 

 

PROC Star
Posts: 290

Re: Extracting word

I've only eyeballed the data since I could not copy it and test it, but I think something like this will work with the cases you've shown. 

 

 

data want;
have = 'Race Black or African American vs White';
want = catx(' ', scan(have, 1, ' '), scan(have, -1, 'vs'));
run;
Solution
‎02-22-2017 02:41 PM
PROC Star
Posts: 7,364

Re: Extracting word

data want;
  set have;;
  CALL SCAN(string, 2, start, length);
  want = substr(string, start, index(string, 'vs')-start);
run;

Art, CEO, AnalystFinder.com

 

Frequent Contributor
Posts: 75

Re: Extracting word

I did the same thing in a very complicated manner, haha! Yours is elegant Smiley Happy
Frequent Contributor
Frequent Contributor
Posts: 76

Re: Extracting word

Thank you all very much for the disuccsion !

 

Ivy 

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 174 views
  • 1 like
  • 4 in conversation