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!
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
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;
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 !!
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;
OK, then it's quiz time. Why does this attempt give the wrong answer?
round = reverse(lpct) in : ('0', '5');
If I guess right. lpct after reversing ,it should be starting with BLANK .
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.
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
:smileylaugh:
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.