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 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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