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
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.
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;
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;
Untested, but wouldn't a simple picture format do the job?
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.