02-01-2018 02:41 PM
I am working on SAS version 9.4. I have a value LBSTRESC='<10.0'. I need to determine how many digits are present in LBSTRESC to the right of the decimal. I thought that the FINDC function would be appropriate. I tried several different combinations of startpos and modifier arguments shown below (and possibly more that produced unexpected results):
lbstresc='<10.0'; num=findc(lbstresc,'.',-1); num=findc(lbstresc,'.','b'); num=findc(lbstresc,'.',-1,'b'); num=findc(lbstresc,'.',-10); num=findc(lbstresc,'.',-10,'b'); num=findc(lbstresc,'.',10,'b'); num=findc(lbstresc,'.',1,'b');
All of these attempts gave me num=0 or num=4. Can someone help me correctly execute this function so that I can get num=2, as in the decimal place is the second value from the right to the left? Thank you!
02-01-2018 03:04 PM
You wrote-"how many digits are present in LBSTRESC to the right of the decimal"
Can someone help me correctly execute this function so that I can get num=2
is the expected num=2 in '<10.0' to the right of the decimal?
02-01-2018 03:20 PM
I’m assuming this isn’t for a single value, so can you post some more data to ensure the solution works with your data.
Another option, use SCAN() with period as the delimiter and single quote and you’ll extract only the numbers behind the decimal. Is the assumption here you’re trying to extract the number?
02-01-2018 03:23 PM
If there is only one decimal
num=length(lbstresc) - findc(lbstresc,'.')+1;
if you have more decimals and need the last then you'll need to provide more details.
02-01-2018 03:36 PM
Looks like you are getting the number you asked for. The period is in the 4th position. If you want to know how many characters follow it then subtract that from the total length. Add 1 if you want to include the period itself.
decimals = length(x) - findc(x,'.') + 1;
Or you could try flipping the the string around instead.
decimals = findc(reverse(trim(x)),'.');
02-01-2018 04:00 PM
02-01-2018 04:14 PM
However, I still don't understand how 4 is what I asked for. I used the 'b' modifier which should read the characters from right to left. But this code works as an alternative.
The position number of the string remains the same (4) when found whether you search from the left or right. Note that the function does not say the number of characters from the start position.
The assumption when using a start position is that you have some reason to start there.
data junk; lbstresc='some.thing.with multiple.'; num=findc(lbstresc,'.',findc(lbstresc,'.')+1); run;
The start position for the outer findc is 1 more than the position of the first decimal . So the found decimal is the one after "thing". Which has position 11 in this example string.
02-01-2018 06:40 PM
I see, thank you so much for the explanation! First time using findc and I have been so confused!
You aren't the first and won't be the last.
I seem to remember similar confusion a few times in the past. And SAS was something like the 7th programming language (plus 5 or 6 dialects of Basic) I learned.