Dear Expert,
Could you please help me concatenate every nth word with a symbol.
Consider that i have a string data like below
"having a common variable to merge by ensures that the"
the expected output should be
"having a common * variable to merge * by ensures that * the"
So after every 3rd word i have to place the symbol(*). like above.
Could you please suggest a solution.
Thanks,
Jag
Put the CATx functions to good use:
data test;
length subStr outStr $200;
inStr = "having a common variable to merge by ensures that the";
do i = 1 by 3 until(Missing(subStr));
subStr = catx(" ", scan(inStr,i), scan(inStr,i+1), scan(inStr,i+2));
outStr = catx(" * ", outStr, subStr);
end;
drop subStr i;
run;
proc print data=test noobs; run;
Put the CATx functions to good use:
data test;
length subStr outStr $200;
inStr = "having a common variable to merge by ensures that the";
do i = 1 by 3 until(Missing(subStr));
subStr = catx(" ", scan(inStr,i), scan(inStr,i+1), scan(inStr,i+2));
outStr = catx(" * ", outStr, subStr);
end;
drop subStr i;
run;
proc print data=test noobs; run;
Here is another way:
data test;
length outStr $200;
inStr = "having a common variable to merge by ensures that the";
outstr=prxchange('s/((\w+ ){3})/$1* /', -1, instr);
run;This is to tell SAS adding * right after the 3rd word, in a literal sense.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.