BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ivy
Quartz | Level 8 Ivy
Quartz | Level 8

 

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

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21
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

5 REPLIES 5
nehalsanghvi
Pyrite | Level 9

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;

 

 

 

 

collinelliot
Barite | Level 11

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;
art297
Opal | Level 21
data want;
  set have;;
  CALL SCAN(string, 2, start, length);
  want = substr(string, start, index(string, 'vs')-start);
run;

Art, CEO, AnalystFinder.com

 

nehalsanghvi
Pyrite | Level 9
I did the same thing in a very complicated manner, haha! Yours is elegant 🙂
Ivy
Quartz | Level 8 Ivy
Quartz | Level 8

Thank you all very much for the disuccsion !

 

Ivy 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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