BookmarkSubscribeRSS Feed
nismail1976
Fluorite | Level 6

Hello everyone,

I have a string variable called part4 that looks something like:

part4.PNG

 

   

 

I am trying to extract the first 24 characters starting from right hand side moving toward the left, how can I accomplish that?

Thanks

4 REPLIES 4
ballardw
Super User

or

extract= substr(x, length(x)-24+1);

 

I did 24 + 1 incase you need to change the 24 based on a variable value.

 

If you have 10 characters and want 4, 10-4=6, but character positions 6,7,8,9 and 10 = 5 characters, so add 1.

 

This will not work if you ask for more characters than exist though.

PGStats
Opal | Level 21

I think this will always work:

 

extract= right(subpad(x, max(1, length(x)-24+1), 24));

PG
ShiroAmada
Lapis Lazuli | Level 10

Try this....

 

 

data want;
  set have;
Latest24months = ifc(length(text)>24,substrn(text,length(text)-23), text);

LATETS24MONTHS_2= ifc(length(text)<=24,TEXT, substrn(text,length(text)-23));

IF LATEST24MONTHS = LATEST24MONTHS_2 THEN TAG=1; run;

 

Choose between latest24months or latest24months_2.  Tag is used to check if both will provide the same result.

 

Hope this helps.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 4 replies
  • 3568 views
  • 6 likes
  • 5 in conversation