Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mcf_getfmttype.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Returns the type of the format
4 @details
5 Returns the type, eg DATE / DATETIME / TIME (based on hard-coded lookup)
6 else CHAR / NUM. The lookup lists live in mf_dttmfmts.sas, which is also
7 usable directly in open code.
8
9 This macro may be extended in the future to support custom formats - this
10 would necessitate a call to `dosubl()` for running a proc format with cntlout.
11
12 The function itself takes the following (positional) parameters:
13
14 | PARAMETER | DESCRIPTION |
15 |---|---|
16 |fmtnm| Format name to be tested. Can be with or without the w.d extension.|
17
18 Usage:
19
20 %mcf_getfmttype()
21
22 data _null_;
23 fmt1=mcf_getfmttype('DATE9.');
24 fmt2=mcf_getfmttype('DATETIME');
25 put (fmt:)(=);
26 run;
27 %put fmt3=%sysfunc(mcf_getfmttype(TIME9.));
28
29 Returns:
30
31 > fmt1=DATE fmt2=DATETIME
32 > fmt3=TIME
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 @li mf_dttmfmts.sas
43
44 <h4> Related Programs </h4>
45 @li mcf_getfmttype.test.sas
46 @li mp_init.sas
47
48 @todo "Custom Format Lookups" To enable site-specific formats, make
49 use of a set of SASJS_FMTLIST_(DATATYPE) global variables.
50
51**/
52
53%macro mcf_getfmttype(wrap=YES
54 ,lib=WORK
55 ,cat=SASJS
56 ,pkg=UTILS
57)/*/STORE SOURCE*/;
58%local i var cmpval found;
59
60%if %mcf_init(mcf_getfmttype)=1 %then %return;
61
62%if &wrap=YES %then %do;
63 proc fcmp outlib=&lib..&cat..&pkg;
64%end;
65
66function mcf_getfmttype(fmtnm $) $8;
67 if substr(fmtnm,1,1)='$' then return('CHAR');
68 else do;
69 /* extract NAME */
70 length fmt $32;
71 fmt=scan(fmtnm,1,'.');
72 do while (
73 substr(fmt,length(fmt),1) in ('1','2','3','4','5','6','7','8','9','0')
74 );
75 if length(fmt)=1 then fmt='W';
76 else fmt=substr(fmt,1,length(fmt)-1);
77 end;
78
79 /* apply lookups - the format lists live in mf_dttmfmts.sas */
80 if cats(fmt) in (%mf_dttmfmts(DATETIME)) then return('DATETIME');
81 else if cats(fmt) in (%mf_dttmfmts(DATE)) then return('DATE');
82 else if cats(fmt) in (%mf_dttmfmts(TIME)) then return('TIME');
83 else return('NUM');
84 end;
85endsub;
86
87%if &wrap=YES %then %do;
88 quit;
89%end;
90
91/* insert the CMPLIB if not already there */
92%let cmpval=%sysfunc(getoption(cmplib));
93%let found=0;
94%do i=1 %to %sysfunc(countw(&cmpval,%str( %(%))));
95 %let var=%scan(&cmpval,&i,%str( %(%)));
96 %if &var=&lib..&cat %then %let found=1;
97%end;
98%if &found=0 %then %do;
99 options insert=(CMPLIB=(&lib..&cat));
100%end;
101
102%mend mcf_getfmttype;