data y ;
input num ;
cards ;
3445.356
666745.546
333.6756
36356356.345
;
how to do like take num variable (only integers) a like see below example remove decimals with in number we have form the decimals and finally variable should be numeric datatype
34.45
66.67
33.3
36.35
So let's see if my understanding is correct.
You want the first two digits of a long number to be in the result to the left of the decimal point.
Then you want the next two digits of a long number to be in the result to the right of the decimal point, but if the original number doesn't have two more digits before the decimal point, then the result should only have the next one digit to the right of the decimal point.
Yes or no?
@thanikondharish wrote:
For example number is 8956.1234
Finally should be like 89.56
Don't take decimals
Yes, fine, that's clear. If the number is 895.61234, we should ignore the numbers to the right of the decimal point and produce the result 89.5? What if the original number is 89.561234??
So 89.561234 would result in 89.5, no rounding here?
Try this:
data want;
set have;
l=floor(log10(num));
if l>2 then
y=input(substrn(num,1,2),2.)+input(substrn(num,3,2),2.)/100;
else if l=2 then y=input(substrn(num,1,2),2.)+input(substrn(num,3,2),2.)/10;
else if l=1 then y=input(substrn(num,1,2),2.)+input(substrn(num,3,2),2.);
drop l;
run;
@thanikondharish wrote:
data y ;
input num ;
cards ;
3445.356
666745.546
333.6756
36356356.345
;how to do like take num variable (only integers) a like see below example remove decimals with in number we have form the decimals and finally variable should be numeric datatype
34.45
66.67
33.3
36.35
Please provide a clearer example of START and End desired result. And possibly the steps you use to get the result.
Your second list of random numbers includes decimals, so I am not sure what you mean by "only integers" or what you have done as there are apparently different rules applied to different ranges of values.
You can get the integer portion of a numeric value using the INT or INTZ functions depending on how you want to treat very small decimal differences from the integer.
@thanikondharish wrote:
Any number first remove decimals next take number like if
Total digits 5 then 12.34
total digits 4 then 12.34
Total digits 3 then 12.3
Total digits 2 then 12.00
And how does the above relate to your posted value of
36356356.345
which would seem to have 8 "total digits" (in your terminology, not mine)
Can we ask why? Is it possible to fix the confusion at some earlier step in the process?
So use INT() to get integer part. Use PUT to convert it it to text. Left align it. Convert trailing spaces to zeros. Read first 4 digits using implied decimal point.
Or use LOG10() to find the magnitude of the number and divide by the appropriate power of 10.
data test ;
input num ;
want=input(translate(put(int(num),32.-L),'0',' '),4.2);
want2=int(int(num)/10**(int(log10(num))-3))/100;
cards ;
3445.356
666745.546
333.6756
36356356.345
;
Obs num want want2 1 3445.36 34.45 34.45 2 666745.55 66.67 66.67 3 333.68 33.30 33.30 4 36356356.35 36.35 36.35
proc format;
picture fmt
low-high='000000.99';
run;
data y ;
input num ;
n=(ceil(log10(num))-2);
want=int(num)/(10**n);
format want fmt.;
cards ;
3445.356
666745.546
333.6756
36356356.345
;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.