Macros for SAS Application Developers
https://github.com/sasjs/core
mcf_length.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Returns the length of a numeric value
4  @details
5  Returns the length, in bytes, of a numeric value. If the value is
6  missing, then 0 is returned.
7 
8  The function itself takes the following (positional) parameters:
9 
10  | PARAMETER | DESCRIPTION |
11  |---|---|
12  | var | variable (or value) to be tested|
13 
14  Usage:
15 
16  %mcf_length(wrap=YES, insert_cmplib=YES)
17 
18  data _null_;
19  ina=1;
20  inb=10000000;
21  inc=12345678;
22  ind=.;
23  outa=mcf_length(ina);
24  outb=mcf_length(inb);
25  outc=mcf_length(inc);
26  outd=mcf_length(ind);
27  put (out:)(=);
28  run;
29 
30  Returns:
31 
32  > outa=3 outb=4 outc=5 outd=0
33 
34  @param [out] wrap= (NO) Choose YES to add the proc fcmp wrapper.
35  @param [out] lib= (work) The output library in which to create the catalog.
36  @param [out] cat= (sasjs) The output catalog in which to create the package.
37  @param [out] pkg= (utils) The output package in which to create the function.
38  Uses a 3 part format: libref.catalog.package
39  @param [out] insert_cmplib= DEPRECATED - The CMPLIB option is checked and
40  values inserted only if needed.
41 
42  <h4> SAS Macros </h4>
43  @li mcf_init.sas
44 
45  <h4> Related Programs </h4>
46  @li mcf_length.test.sas
47  @li mp_init.sas
48 
49 **/
50 
51 %macro mcf_length(wrap=NO
52  ,insert_cmplib=DEPRECATED
53  ,lib=WORK
54  ,cat=SASJS
55  ,pkg=UTILS
56 )/*/STORE SOURCE*/;
57 %local i var cmpval found;
58 %if %mcf_init(mcf_length)=1 %then %return;
59 
60 %if &wrap=YES %then %do;
61  proc fcmp outlib=&lib..&cat..&pkg;
62 %end;
63 
64 function mcf_length(var);
65  if var=. then len=0;
66  else if missing(var) or trunc(var,3)=var then len=3;
67  else if trunc(var,4)=var then len=4;
68  else if trunc(var,5)=var then len=5;
69  else if trunc(var,6)=var then len=6;
70  else if trunc(var,7)=var then len=7;
71  else len=8;
72  return(len);
73 endsub;
74 
75 %if &wrap=YES %then %do;
76  quit;
77 %end;
78 
79 /* insert the CMPLIB if not already there */
80 %let cmpval=%sysfunc(getoption(cmplib));
81 %let found=0;
82 %do i=1 %to %sysfunc(countw(&cmpval,%str( %(%))));
83  %let var=%scan(&cmpval,&i,%str( %(%)));
84  %if &var=&lib..&cat %then %let found=1;
85 %end;
86 %if &found=0 %then %do;
87  options insert=(CMPLIB=(&lib..&cat));
88 %end;
89 
90 %mend mcf_length;