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:

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 1496 views
  • 0 likes
  • 7 in conversation