BookmarkSubscribeRSS Feed
kli15
Calcite | Level 5

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!

11 REPLIES 11
novinosrin
Tourmaline | Level 20

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?

 

 

novinosrin
Tourmaline | Level 20

Are you looking for something like:

data want;
lbstresc='<10.0000';
num=findc(lbstresc,'.');
count=lengthn(substr(lbstresc,num+1));
run;
kli15
Calcite | Level 5

This helped point me in the right direction. Thanks!

Reeza
Super User

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?

 

 

ballardw
Super User

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.

Tom
Super User Tom
Super User

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)),'.');
kli15
Calcite | Level 5
This helped point me in the right direction. Thanks!
kli15
Calcite | Level 5
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.
ballardw
Super User

@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.

 

kli15
Calcite | Level 5
I see, thank you so much for the explanation! First time using findc and I have been so confused!
ballardw
Super User

@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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 11 replies
  • 688 views
  • 0 likes
  • 5 in conversation