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

Hi,  is there a quick way to substring from the right but skip a few digits.

If I have 2345001  I want to get 2345 .. I want to only remove the last 3 digits...  the length of the field is not uniform, soucl be 8 digits or 10 or 9, etc.  but th eonly thing sore sure is that I want to remove the last 3 digits..  Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

myStr =  substr(myStr, 1, length(myStr) - 3);

PG

View solution in original post

8 REPLIES 8
PGStats
Opal | Level 21

myStr =  substr(myStr, 1, length(myStr) - 3);

PG
monei011
Calcite | Level 5

Another approach in these sorts of situations is to use the substring function with the reverse function

mystr =  reverse(substr(strip(reverse(mystr)),4));

Tom
Super User Tom
Super User

Is your variable a number or a character string?

137  data _null_;

138    x=2345001 ;

139    y=int(x/1000);

140    put x= y=;

141  run;

x=2345001 y=2345

Haikuo
Onyx | Level 15

The more, the merrier:

data _null_;

  mystr="2345001";

  new_str=prxchange('s/(^\w+)(\w{3}$)/$1/',-1,strip(mystr));

  put mystr= new_str=;

run;

Haikuo

PGStats
Opal | Level 21

The simpler, the better :smileysilly:

data _null_;

  mystr="  2345001     ";

  new_str = prxchange('s/\d{3}\s*$//', 1, mystr);

  put mystr= new_str=;

run;

PG

PG
Haikuo
Onyx | Level 15

Better indeed!

Haikuo

monei011
Calcite | Level 5

Might be better use of prxchange, but not exactly readable nor intuitively obvious what the code is doing.

I'd want a comment explaining what was being done and why so that my BAU support junior could understand what the code was doing.

PGStats
Opal | Level 21

It reads: Replace (s) a succession of exactly 3 ({3}) digits (\d) followed by any amount (*) of white space (\s) and occuring at the end of the string ($) with nothing(//). Do this only once.

PG

PG

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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