FINDC not producing expected results

Reply
Occasional Contributor
Posts: 6

FINDC not producing expected results

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!

PROC Star
Posts: 826

Re: FINDC not producing expected results

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?

 

 

PROC Star
Posts: 826

Re: FINDC not producing expected results

Are you looking for something like:

data want;
lbstresc='<10.0000';
num=findc(lbstresc,'.');
count=lengthn(substr(lbstresc,num+1));
run;
Occasional Contributor
Posts: 6

Re: FINDC not producing expected results

Posted in reply to novinosrin

This helped point me in the right direction. Thanks!

Super User
Posts: 21,464

Re: FINDC not producing expected results

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?

 

 

Super User
Posts: 12,148

Re: FINDC not producing expected results

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.

Super User
Super User
Posts: 7,380

Re: FINDC not producing expected results

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)),'.');
Occasional Contributor
Posts: 6

Re: FINDC not producing expected results

This helped point me in the right direction. Thanks!
Occasional Contributor
Posts: 6

Re: FINDC not producing expected results

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.
Super User
Posts: 12,148

Re: FINDC not producing expected results


kli15 wrote:
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.

Consider

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.

 

Occasional Contributor
Posts: 6

Re: FINDC not producing expected results

I see, thank you so much for the explanation! First time using findc and I have been so confused!
Super User
Posts: 12,148

Re: FINDC not producing expected results


kli15 wrote:
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.

Ask a Question
Discussion stats
  • 11 replies
  • 141 views
  • 0 likes
  • 5 in conversation