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

Hi,

Im trying to keep the last letter of a text string that has various lengths in order to create a new variable.

The example is below

Datalines

abcdefg

abcdef

abbbbbnn

dddeeegg

Results

g

f

n

g

So all i need is just the last character.

Thanks!

Ryan

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Always nice to have choices:

data want;

set have;

length result $ 1;

result = left(reverse(text));

run;

View solution in original post

8 REPLIES 8
stat_sas
Ammonite | Level 13

data want;
set have;
result=substr(text,length(strip(text))-1,1);
run;

rjnc13
Calcite | Level 5

Thanks for your quick response, very much appreciated

Astounding
PROC Star

Always nice to have choices:

data want;

set have;

length result $ 1;

result = left(reverse(text));

run;

rjnc13
Calcite | Level 5

Thanks for the speedy response!

Orsini
Fluorite | Level 6

Gosh you folks are fast...

From Cody's SAS Functions by Example:

data have;                                        

   infile datalines4;                              

   input charvar1 $;                               

   list;                                           

   LastChar = substr(charvar1,length(charvar1),1); 

   datalines4;                                     

abcdefg                                            

abcdef                                             

abbbbbnn                                           

dddeeegg                                           

;;;;                                               

run;                                             

Astounding
PROC Star

A couple of hidden "features" here ...

Note that LENGTH never returns 0.  If the incoming string is blank, it returns 1 so the second argument to SUBSTR will still be valid.

Also note that the LENGTH statement should be added.  When SUBSTR creates a new variable, the length will not be $1, but will be the length of the original string.  You can easily see that by running a PROC CONTENTS.

Haikuo
Onyx | Level 15

Another way:

data have;                                       

infile datalines4;                             

input charvar1 $ 8.

lastchar=prxchange('s/(.+)([a-zA-Z])$/$2/o', -1, trim(charvar1));

datalines4;                                    

abcdefg                                           

abcdef                                            

abbbbbnn                                          

dddeeegg 

h

;;;;                                              

run;

SASKiwi
PROC Star

Let me count the ways...the number on the end of the REVERS format must be at least as long as the input variable

data _null_;

a = 'ABCD   ';

attrib LastChar length= $1;

LastChar = put(a, $REVERS100.);

put _all_;

run;

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!

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
  • 8 replies
  • 17267 views
  • 6 likes
  • 6 in conversation