BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jerry898969
Pyrite | Level 9

I'm trying to get the individual numbers after the decimal place and i'm not sure how to do it.

Say I have .003  I want to pull out the first 0 in d1 and the second to d2 and 3 into d3.

Not sure if the mod function would be the way to go or not.

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

FriedEgg,  Tom's method correctly deals with negative numbers .. it simply keeps the minus sign.  If the minus sign isn't desired by the OP, a simple solution would be to add an abs function.  e.g.:

data want;

  input x;

  array digit(3);

  do place=1 to 3 ;

    digit(place)=abs(mod(int(x*10**place),10));

  end;

  put;

cards;

.003

.123

12.345

-12.345

;

proc print;

run;

View solution in original post

7 REPLIES 7
art297
Opal | Level 21

There may be an easier way, but you can always use brute force. e.g.,

data want;

  input x;

  d1=int(x*10);

  d2=int(x*100-(int(x*10)*10));

  d3=int(x*1000-(int(x*100)*10));

  cards;

.003

.123

;

Tom
Super User Tom
Super User

This is a math problem. Here is one way using powers of 10, INT and MOD functions.

data _null_;

  input x;

  put x= @;

  do place=1 to 3 ;

    digit=mod(int(x*10**place),10);

    put digit= @;

  end;

  put;

cards;

.003

.123

12.345

;

Try it starting from 0 or minus one.

jerry898969
Pyrite | Level 9

Thanks for both the answers. 

Tom how can I put the answer in 3 seperate columns for each row?

art297
Opal | Level 21

Using Tom's method is the following what you are looking for?

data want;

  input x;

  array digit(3);

  do place=1 to 3 ;

    digit(place)=mod(int(x*10**place),10);

  end;

  put;

cards;

.003

.123

12.345

;

proc print;

run;

FriedEgg
SAS Employee

If you have a negative number this should have one minor flaw.  I cannot test but I believe that if you have say -1.003 then using this method digit3=-3 which you probably do not want. You will probably want to account for this.

art297
Opal | Level 21

FriedEgg,  Tom's method correctly deals with negative numbers .. it simply keeps the minus sign.  If the minus sign isn't desired by the OP, a simple solution would be to add an abs function.  e.g.:

data want;

  input x;

  array digit(3);

  do place=1 to 3 ;

    digit(place)=abs(mod(int(x*10**place),10));

  end;

  put;

cards;

.003

.123

12.345

-12.345

;

proc print;

run;

jerry898969
Pyrite | Level 9

Thank you to everyone for your suggestions. 

I used Tom and Art's suggestions.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1872 views
  • 6 likes
  • 4 in conversation