Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
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()
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= (YES) Choose NO to omit 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
40 <h4> SAS Macros </h4>
41 @li mcf_init.sas
42
43 <h4> Related Programs </h4>
44 @li mcf_length.test.sas
45 @li mp_init.sas
46
47**/
48
49%macro mcf_length(wrap=YES
50 ,lib=WORK
51 ,cat=SASJS
52 ,pkg=UTILS
53)/*/STORE SOURCE*/;
54%local i var cmpval found;
55%if %mcf_init(mcf_length)=1 %then %return;
56
57%if &wrap=YES %then %do;
58 proc fcmp outlib=&lib..&cat..&pkg;
59%end;
60
61function mcf_length(var);
62 if var=. then len=0;
63 else if missing(var) or trunc(var,3)=var then len=3;
64 else if trunc(var,4)=var then len=4;
65 else if trunc(var,5)=var then len=5;
66 else if trunc(var,6)=var then len=6;
67 else if trunc(var,7)=var then len=7;
68 else len=8;
69 return(len);
70endsub;
71
72%if &wrap=YES %then %do;
73 quit;
74%end;
75
76/* insert the CMPLIB if not already there */
77%let cmpval=%sysfunc(getoption(cmplib));
78%let found=0;
79%do i=1 %to %sysfunc(countw(&cmpval,%str( %(%))));
80 %let var=%scan(&cmpval,&i,%str( %(%)));
81 %if &var=&lib..&cat %then %let found=1;
82%end;
83%if &found=0 %then %do;
84 options insert=(CMPLIB=(&lib..&cat));
85%end;
86
87%mend mcf_length;