BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
aashish_jain
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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;

Capture.JPG

 

View solution in original post

12 REPLIES 12
PeterClemmensen
Tourmaline | Level 20

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?

aashish_jain
Obsidian | Level 7
You can give either Both are acceptable to me.
But former would be preferable
PeterClemmensen
Tourmaline | Level 20

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;

 

aashish_jain
Obsidian | Level 7
Thank for the Reply .
But What if X is like
X=1321462
PeterClemmensen
Tourmaline | Level 20

What would you like the format to return then?

aashish_jain
Obsidian | Level 7
I want the output like this
IF X=1321462
OUTPUT=Thirteen lakh twenty-one thousand four hundred sixty two
Patrick
Opal | Level 21

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;

Capture.JPG

 

aashish_jain
Obsidian | Level 7

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.

aashish_jain
Obsidian | Level 7
While using PROC FORMAT. It didn't let me choose the same name of the format i.e lakh_words.
So I used lakh_wordsA.
Patrick
Opal | Level 21

@aashish_jain 

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;
 

 

aashish_jain
Obsidian | Level 7
Thank you again for taking the time to figure out and making the code dynamic.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 12 replies
  • 2446 views
  • 3 likes
  • 3 in conversation