turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- General Programming
- /
- FINDC not producing expected results

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

3 weeks ago

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!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to kli15

3 weeks ago

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?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to kli15

3 weeks ago

Are you looking for something like:

```
data want;
lbstresc='<10.0000';
num=findc(lbstresc,'.');
count=lengthn(substr(lbstresc,num+1));
run;
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to novinosrin

3 weeks ago

This helped point me in the right direction. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to kli15

3 weeks ago

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?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to kli15

3 weeks ago

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to kli15

3 weeks ago

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)),'.');`

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

3 weeks ago

This helped point me in the right direction. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

3 weeks ago

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to kli15

3 weeks ago

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ballardw

2 weeks ago

I see, thank you so much for the explanation! First time using findc and I have been so confused!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to kli15

2 weeks ago

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.