how to get the last two words using scan function in SAS?
if i have visit as
Visit 2 Week 4
Visit 12 Week 42
visit 1 day 1
visit 16 week 52
i want the ouptut as
Week 4
Week 42
Week 52
Thanks in advance
With Scan Function:
data have;
input string $ 1-20;
datalines;
Visit 2 Week 4
Visit 12 Week 42
visit 1 day 1
visit 16 week 52
;
data want;
set have;
lasttwo=cat(scan(string, -1), ' ', scan(string, -2));
run;
Result:
string lasttwo Visit 2 Week 4 Week 4 Visit 12 Week 42 Week 42 visit 1 day 1 day 1 visit 16 week 52 week 52
With Scan Function:
data have;
input string $ 1-20;
datalines;
Visit 2 Week 4
Visit 12 Week 42
visit 1 day 1
visit 16 week 52
;
data want;
set have;
lasttwo=cat(scan(string, -1), ' ', scan(string, -2));
run;
Result:
string lasttwo Visit 2 Week 4 Week 4 Visit 12 Week 42 Week 42 visit 1 day 1 day 1 visit 16 week 52 week 52
@Aayushi_17 wrote:
how to get the last two words using scan function in SAS?
if i have visit as
Visit 2 Week 4
Visit 12 Week 42
visit 1 day 1
visit 16 week 52
i want the ouptut as
Week 4
Week 42
????
Week 52
Thanks in advance
So you want output only if the value contains the word week? Or why is the fourth line empty?
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!
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.