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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

8 REPLIES 8
Linlin
Lapis Lazuli | Level 10

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;

yeshwanth
Fluorite | Level 6

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 !!

Haikuo
Onyx | Level 15

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;

Astounding
PROC Star

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

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

Ksharp
Super User

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

Astounding
PROC Star

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.

PGStats
Opal | Level 21

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
Haikuo
Onyx | Level 15

:smileylaugh:

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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