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
- /
- SAS Procedures
- /
- How to pull out each place of a decimal value

- 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
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

09-12-2011 04:43 PM

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

Accepted Solutions

Solution

09-13-2011
12:05 PM

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

Posted in reply to FriedEgg

09-13-2011 12:05 PM

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;

All Replies

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

Posted in reply to jerry898969

09-12-2011 05:56 PM

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

;

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

Posted in reply to jerry898969

09-12-2011 06:10 PM

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.

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

09-13-2011 08:54 AM

Thanks for both the answers.

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

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

Posted in reply to jerry898969

09-13-2011 09:44 AM

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;

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

Posted in reply to art297

09-13-2011 11:45 AM

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.

Solution

09-13-2011
12:05 PM

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

Posted in reply to FriedEgg

09-13-2011 12:05 PM

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;

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

Posted in reply to art297

09-13-2011 12:12 PM

Thank you to everyone for your suggestions.

I used Tom and Art's suggestions.