SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Aayushi_17
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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 

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

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 
andreas_lds
Jade | Level 19

@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?

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 3832 views
  • 0 likes
  • 3 in conversation