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.
data want; set have;; CALL SCAN(string, 2, start, length); want = substr(string, start, index(string, 'vs')-start); run;
Art, CEO, AnalystFinder.com
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;
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;
data want; set have;; CALL SCAN(string, 2, start, length); want = substr(string, start, index(string, 'vs')-start); run;
Art, CEO, AnalystFinder.com
Thank you all very much for the disuccsion !
Ivy
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.