DATA Step, Macro, Functions and more

How to keep the last character of a text string and use it for a new variable?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 16
Accepted Solution

How to keep the last character of a text string and use it for a new variable?

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


Accepted Solutions
Solution
‎11-19-2014 01:09 PM
Super User
Posts: 5,498

Re: How to keep the last character of a text string and use it for a new variable?

Always nice to have choices:

data want;

set have;

length result $ 1;

result = left(reverse(text));

run;

View solution in original post


All Replies
Trusted Advisor
Posts: 1,228

Re: How to keep the last character of a text string and use it for a new variable?

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

Occasional Contributor
Posts: 16

Re: How to keep the last character of a text string and use it for a new variable?

Thanks for your quick response, very much appreciated

Solution
‎11-19-2014 01:09 PM
Super User
Posts: 5,498

Re: How to keep the last character of a text string and use it for a new variable?

Always nice to have choices:

data want;

set have;

length result $ 1;

result = left(reverse(text));

run;

Occasional Contributor
Posts: 16

Re: How to keep the last character of a text string and use it for a new variable?

Posted in reply to Astounding

Thanks for the speedy response!

Occasional Contributor
Posts: 13

Re: How to keep the last character of a text string and use it for a new variable?

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;                                             

Super User
Posts: 5,498

Re: How to keep the last character of a text string and use it for a new variable?

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.

Respected Advisor
Posts: 3,156

Re: How to keep the last character of a text string and use it for a new variable?

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;

Super User
Posts: 3,252

Re: How to keep the last character of a text string and use it for a new variable?

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;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 5712 views
  • 6 likes
  • 6 in conversation