Desktop productivity for business analysts and programmers

Capturing the last character in a variable value

Accepted Solution Solved
Reply
N/A
Posts: 1
Accepted Solution

Capturing the last character in a variable value

Hello all,

This may be a simple question, but I'm quite new to SAS.  I'm trying to do a few things, but before I can do any of the substantive stuff, I first need to identify the cases where the values in a variable called LPCT end in a 0 or 5.  The values in LPCT differ in length though so I don't know how to specify the position of the last integer in the value.   Is there is a command for "last character" or "ends in"  in SAS language?

I want to to this:

IF LPCT ends-in 0 OR LPCT ends-in THEN ROUNDED=1;

ELSE ROUNDED=0;

but I don't know the real SAS-words for ends-in.  Some version of the SUBSTR function would be great too, but again, the values of LPCT are of varyng lengths so I can't specify a numeric position for the integer I want to capture...I need to just tell SAS to take the last number in the variable's value somehow.

I'm not sure if I'm being clear but I hope someone has some suggestion!


Accepted Solutions
Solution
‎06-23-2014 05:52 PM
Respected Advisor
Posts: 4,609

Re: Capturing the last character in a variable value

All solutions proposed so far have small weaknesses ...

With REVERSE, trailing spaces will become leading spaces, so you would want to TRIM first.

You would still need to compare the resulting string with "0" and "5"

The pipe character is not required in the pattern

data y1;

     input id lpct $;

datalines;

1 sam0

2 alex5

3 jason1

;

DATA WANT;

     SET Y1;

     ROUNDED = CHAR(lpct, LENGTH(lpct)) in ("0", "5");

run;

PG

PG

View solution in original post


All Replies
Super Contributor
Posts: 1,636

Re: Capturing the last character in a variable value

data have;

input lpct;

cards;

1

2

2.5

30

8

;

data want;

  set have;

  if first(reverse(lpct)) in ('0','5') then rounded=1;

   else rounded=0;

proc print;run;

Contributor
Posts: 22

Re: Capturing the last character in a variable value

data y1;

input id LPCT $;

cards;

1 sam0

2 alex5

3 jason0

;

run;

data y2;

set y1;

required_output=substr(lpct,length(lpct),1);

run;

Hope this helps !!

Respected Advisor
Posts: 3,124

Re: Capturing the last character in a variable value

The more, the merrier:

data y1;

     input id LPCT $;

     cards;

1 sam0

2 alex5

3 jason1

;

DATA WANT;

     SET Y1;

     ROUND=PRXMATCH('/[5|0]$/',strip(lpct))>0;

run;

Respected Advisor
Posts: 4,995

Re: Capturing the last character in a variable value

OK, then it's quiz time.  Why does this attempt give the wrong answer?

round = reverse(lpct) in : ('0', '5');

Grand Advisor
Posts: 9,592

Re: Capturing the last character in a variable value

If I guess right. lpct after reversing ,it should be starting with BLANK .

Respected Advisor
Posts: 4,995

Re: Capturing the last character in a variable value

Yes, that's the issue.  I wasn't sure anyone would answer on this, since PG Stats hinted at the problem with his comment about Linlin's solution.  When reversing generates a leading blank, both my quiz and Linlin's solution face this issue.

Solution
‎06-23-2014 05:52 PM
Respected Advisor
Posts: 4,609

Re: Capturing the last character in a variable value

All solutions proposed so far have small weaknesses ...

With REVERSE, trailing spaces will become leading spaces, so you would want to TRIM first.

You would still need to compare the resulting string with "0" and "5"

The pipe character is not required in the pattern

data y1;

     input id lpct $;

datalines;

1 sam0

2 alex5

3 jason1

;

DATA WANT;

     SET Y1;

     ROUNDED = CHAR(lpct, LENGTH(lpct)) in ("0", "5");

run;

PG

PG
Respected Advisor
Posts: 3,124

Re: Capturing the last character in a variable value

:smileylaugh:

☑ This topic is SOLVED.

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

Discussion stats
  • 8 replies
  • 410 views
  • 0 likes
  • 7 in conversation