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
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;
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
;
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.
Thanks for both the answers.
Tom how can I put the answer in 3 seperate columns for each row?
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;
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.
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;
Thank you to everyone for your suggestions.
I used Tom and Art's suggestions.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.