And just for fun here a RegEx option.
data sample;
infile datalines truncover;
input have_string $50.;
/* define want_string with a length long enough to ensure there is never string truncation */
if 0 then want_string=have_string||'and';
want_string=prxchange('s/ *, *([^,]+)$/ and $1/oi',1,strip(have_string));
datalines;
Alpha beta , for a , for b , for c
Alpha beta , for a , for b, for c
Alpha beta , for a , for b,for c
;
proc print data=sample;
run;
You can use next code:
ix = index(varname,',',-1); * -1 scans right to left;
if ix > 0 then /* 0 means no comma found */
varname = substr(varname,1,ix-1) || ' and ' || substr(varname, ix+1);
You may nead assign new length to the varnamre as length('and") > lenght(',').
And just for fun here a RegEx option.
data sample;
infile datalines truncover;
input have_string $50.;
/* define want_string with a length long enough to ensure there is never string truncation */
if 0 then want_string=have_string||'and';
want_string=prxchange('s/ *, *([^,]+)$/ and $1/oi',1,strip(have_string));
datalines;
Alpha beta , for a , for b , for c
Alpha beta , for a , for b, for c
Alpha beta , for a , for b,for c
;
proc print data=sample;
run;
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!
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.
Ready to level-up your skills? Choose your own adventure.