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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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