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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.