BookmarkSubscribeRSS Feed
Dogo23
Quartz | Level 8

Hi all,

 

I have a field with string values than have various ranges of length. I want to extract an ID hidden within the string, and it seems that it will be the same character distance from the right side of the string, so instead of doing substr from the left side, I want to start counting back from the right.

 

Also, I should note that even though it seems to be consistently the same distance from the right, there are a few values  at the end that I want removed, so I imagine I'd have to specify the number of characters to bring back.  

 

Here is what the field value looks like, I want to extract the the part of the string that is in bold. Let's assume this field is called "code".

 

alamessage_email_announcenews_NA_PR_20170301_cta_OP00423231412210381PR_M-M32321771C

2 REPLIES 2
Reeza
Super User

REVERSE() function will reverse the string.

SCAN() with negative numbers will extract from the right of the string. 

I don't recall if SUBSTR() takes negative numbers, you can look into that

LENGTH() will return the length of the string if that's required.

 

One data point isn't helpful for generating a rule. 

 


@schlotty23 wrote:

Hi all,

 

I have a field with string values than have various ranges of length. I want to extract an ID hidden within the string, and it seems that it will be the same character distance from the right side of the string, so instead of doing substr from the left side, I want to start counting back from the right.

 

Also, I should note that even though it seems to be consistently the same distance from the right, there are a few values  at the end that I want removed, so I imagine I'd have to specify the number of characters to bring back.  

 

Here is what the field value looks like, I want to extract the the part of the string that is in bold. Let's assume this field is called "code".

 

alamessage_email_announcenews_NA_PR_20170301_cta_OP00423231412210381PR_M-M32321771C


 

Astounding
PROC Star

Perhaps easiest:

 

length ID $ 15;

ID = substr(code, length(code)-30);

 

That's assuming that I counted properly and there are 15 "characters to be ignored" at the end, as well as 15 characters in ID.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 1768 views
  • 0 likes
  • 3 in conversation