BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Solph
Pyrite | Level 9

Hi,

 

I tried to find text from the right before a underscore, but the code didn't work. Any help is much appreciated. Thanks.

 

data one;                                  
input name $1-18;   
cards;
abc_cost_mean
abc_visit_meanu Claim_Visits_Sum Claim_Cost_all_Sum ; data two; set one; name=compress(name); length name2 $10; CALL SCAN( name, -1, last_pos, last_length); name2 = substrn( name, last_pos , last_length); run; proc print; run;

 

Edited. Desired results as below, for name (original) and name2 (derived): 

abc_cost_mean        mean

abc_visit_meanu      meanu

Claim_Visits_Sum     Sum

Claim_Cost_all_Sum   Sum

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star
Your problem title sounds different compared to your description. Based on the description only, you could get the right result simply:

name2 = scan(name, -1, '_') ;

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

What is your desired result here?

Solph
Pyrite | Level 9

Thanks for the reminder. Now added in the original post.

PeterClemmensen
Tourmaline | Level 20

Do like this

 

data one;                                  
input name $1-30;   
cards;
abc_cost_meanabc_visit_meanu
Claim_Visits_Sum
Claim_Cost_all_Sum
;

data two;
    set one;
    name2=substr(name, findc(name,'_','b')+1);
run;
Astounding
PROC Star
Your problem title sounds different compared to your description. Based on the description only, you could get the right result simply:

name2 = scan(name, -1, '_') ;
Solph
Pyrite | Level 9

I thought I tried scan(var, -1,"_") and it didn't work. But apparently I used it wrong. Your code works. Thanks a lot. And thanks all for the help.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 5 replies
  • 794 views
  • 0 likes
  • 3 in conversation