BookmarkSubscribeRSS Feed
Q1983
Lapis Lazuli | Level 10

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

5 REPLIES 5
Astounding
PROC Star

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.

PGStats
Opal | Level 21

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
pearsoninst
Pyrite | Level 9
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;


LinusH
Tourmaline | Level 20

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

Data never sleeps
PGStats
Opal | Level 21

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 922 views
  • 0 likes
  • 5 in conversation