Hi Guys,
Suppose I have data like this.
DATA HAVE;
X=100000;
Y=1000000;
Z=1500000;
RUN;
and so on.
I want these numbers to be presented in a word format like
The value of X Would be One Lakh, Y Would be like Ten Lakh
In SAS I have tried WORD. and WORDS. Format,But all these formats are giving Output like
X=one hundred thousand
Y=one million.
Also attaching the screenshot of the output from functions I have used i.e. Words.
I want the output in Indian Format.
Something like below could work. I haven't looked up what the next unit after lakh is so if your number can become that big then you would need to extend the function accordingly.
proc fcmp outlib=work.functions.myfuncs;
function lakh_words(in_num) $;
length lakh_words $150;
_lakh=abs(int(in_num/100000));
if _lakh>0 then
do;
if abs(in_num) ne in_num then
do;
lakh_words='minus';
in_num=abs(in_num);
end;
lakh_words=catx(' ',lakh_words, put(_lakh,words60.),'lakh'
,put((in_num- _lakh*100000),words150.)
);
end;
else
lakh_words=put(in_num,words150.);
return(lakh_words);
endsub;
run;
options cmplib=(work.functions);
/* Create a format using the function created by the FCMP procedure. */
proc format;
value lakh_words(default=150)
other=[lakh_words()]
;
run;
data test;
input numvar;
datalines;
462
1462
21462
321462
1321462
51321462
51321462.3
-51321462.3
;
proc print data=test;
format numvar lakh_words. ;
var numvar / style(data)=[textalign= left];
run;
Hi and welcome to the SAS Community 🙂
Do you want to create a new character variable to hold this value or simply format the existing numeric value?
Using a picture format..
proc format;
picture ind other='00009 Lakh' (mult=.00001);
run;
data have;
x=100000;
y=1000000;
z=1500000;
format x y z ind.;
run;
What would you like the format to return then?
Something like below could work. I haven't looked up what the next unit after lakh is so if your number can become that big then you would need to extend the function accordingly.
proc fcmp outlib=work.functions.myfuncs;
function lakh_words(in_num) $;
length lakh_words $150;
_lakh=abs(int(in_num/100000));
if _lakh>0 then
do;
if abs(in_num) ne in_num then
do;
lakh_words='minus';
in_num=abs(in_num);
end;
lakh_words=catx(' ',lakh_words, put(_lakh,words60.),'lakh'
,put((in_num- _lakh*100000),words150.)
);
end;
else
lakh_words=put(in_num,words150.);
return(lakh_words);
endsub;
run;
options cmplib=(work.functions);
/* Create a format using the function created by the FCMP procedure. */
proc format;
value lakh_words(default=150)
other=[lakh_words()]
;
run;
data test;
input numvar;
datalines;
462
1462
21462
321462
1321462
51321462
51321462.3
-51321462.3
;
proc print data=test;
format numvar lakh_words. ;
var numvar / style(data)=[textalign= left];
run;
Thank You So much for the help.
The code worked like a charm.
The only Limitation I figured out that it didn't work for values in Crores, for that you already told to adjust the formulas.
Thanks Again.
If I Googled that right then the units change by 10 power 2.
Below a code version which also covers crore and arab. If you need even more units then the only thing you should need to do is amending the array statement.
proc fcmp outlib=work.functions.myfuncs;
function ind_num_terms(in_num) $;
length ind_num_in_words $150;
_in_num_abs=abs(in_num);
_ind_num_part=int(_in_num_abs/100000);
if _ind_num_part>0 then
do;
array ind_terms {3} $5 ('lakh','crore','arab');
_units=ceil(length(strip(put(_ind_num_part,16.)))/2);
_remainder=_ind_num_part;
do _i=min(_units,dim(ind_terms)) to 1 by -1;
ind_num_in_words=catx(' ',ind_num_in_words,put(int(_remainder/100**(_i-1)),words60.),ind_terms[_i]);
_remainder=_remainder-int(_remainder/100**(_i-1))*(100**(_i-1));
end;
ind_num_in_words=catx(' ',ifc(_in_num_abs=in_num,' ','minus')
,ind_num_in_words,put((_in_num_abs- _ind_num_part*100000),words150.)
);
end;
else
ind_num_in_words=put(in_num,words150.);
return(ind_num_in_words);
endsub;
run;
options cmplib=(work.functions);
/* Create a format using the function created by the FCMP procedure. */
proc format;
value ind_num_terms(default=150)
other=[ind_num_terms()]
;
run;
data test;
input numvar;
datalines;
1
21
321
4321
54321
654321
7654321
87654321
987654321
1987654321
21987654321
-21462
-51321462
-51321462.3
;
proc print data=test;
format numvar ind_num_terms.;
var numvar / style(data)=[textalign= left];
run;
@Patrick very nice!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.