## Numeric formats in datasteps

Regular Contributor
Posts: 179

# Numeric formats in datasteps

data have;

a=99.99999999

b=370.86899;

run;

I want to convert a to read 99M and b to read 370K.  So in each case I want to read everything through the decimal point and then add the desired letters of either M or K.  I read about the picture function in sas though I have not used it much

Super User
Posts: 6,785

## Re: Numeric formats in datasteps

And how do you know whether to use a "K" or an "M"?  I'm going to guess at your answer and suggest some code that would become part of a DATA step.

a_text = put(a, best16.);

a_text = left(a_text);

m_or_k = length(a_text) - index(a_text, '.') - 2;

a_text = left(put(int(a), 5.)) || substr('KM', m_or_k/3, 1);

If you would like to clarify what your method is for assigning "K" or "M", I might need to adjust the code.

Good luck.

Posts: 5,540

## Re: Numeric formats in datasteps

Since you don't want to round the numbers but simply drop the decimals, I think you should define function formats such as

proc fcmp outlib=sasuser.fcmp.format;
function intk(x) \$8;
return (cats(int(x),"K"));
endsub;
function intm(x) \$8;
return (cats(int(x),"M"));
endsub;
run;

options cmplib=sasuser.fcmp;

proc format;
value intk (default=8) other = [intk()];
value intm (default=8) other = [intm()];
run;

data have;
a=99.99999999;
b=370.86899;
format a intm. b intk.;
run;

proc print data=have; run;
PG
Frequent Contributor
Posts: 108

## Re: Numeric formats in datasteps

A different Approach

data have;
input ID\$ Number\$;
NewID = INDEX(Number,'.');
newid1 = substr(Number,1,NewID);
target='.';
replacement='M';
replacement1='K';
if NewID = 3 then do ;
Ineedthisvalue=transtrn(newid1,target,replacement);
end;
Else do;
Ineedthisvalue=transtrn(newid1,target,replacement1);
end;
drop target replacement replacement1 newid1;
datalines;
A 99.99999999
B 370.86899
;
run;
Proc Print data = have;
run;

Super User
Posts: 5,884

## Re: Numeric formats in datasteps

Posted in reply to pearsoninst

Untested, but wouldn't a simple picture format do the job?

Data never sleeps
Posts: 5,540

## Re: Numeric formats in datasteps

Picture formats don't work all the time, look at pick. and picm. below

proc fcmp outlib=sasuser.fcmp.format;
function intk(x) \$8;
return (cats(int(x),"K"));
endsub;
function intm(x) \$8;
return (cats(int(x),"M"));
endsub;
run;

options cmplib=sasuser.fcmp;

proc format;
value intk (default=8) other = [intk()];
value intm (default=8) other = [intm()];
picture pick other = "009K";
picture picm other = "009M";
run;

data have;
a=99.99999999; aa=a;
b=370.86899; bb=b;
format a intm. b intk. aa picm. bb pick.;
run;

proc print data=have; run;
Obs     a      aa      b       bb

1     99M    100M    370K    370K
PG
Discussion stats
• 5 replies
• 272 views
• 0 likes
• 5 in conversation