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.
7
8 This macro may be extended in the future to support custom formats - this
9 would necessitate a call to `dosubl()` for running a proc format with cntlout.
10
11 The function itself takes the following (positional) parameters:
12
13 | PARAMETER | DESCRIPTION |
14 |---|---|
15 |fmtnm| Format name to be tested. Can be with or without the w.d extension.|
16
17 Usage:
18
19 %mcf_getfmttype()
20
21 data _null_;
22 fmt1=mcf_getfmttype('DATE9.');
23 fmt2=mcf_getfmttype('DATETIME');
24 put (fmt:)(=);
25 run;
26 %put fmt3=%sysfunc(mcf_getfmttype(TIME9.));
27
28 Returns:
29
30 > fmt1=DATE fmt2=DATETIME
31 > fmt3=TIME
32
33 @param [out] wrap= (YES) Choose NO to omit the proc fcmp wrapper.
34 @param [out] lib= (work) The output library in which to create the catalog.
35 @param [out] cat= (sasjs) The output catalog in which to create the package.
36 @param [out] pkg= (utils) The output package in which to create the function.
37 Uses a 3 part format: libref.catalog.package
38
39 <h4> SAS Macros </h4>
40 @li mcf_init.sas
41
42 <h4> Related Programs </h4>
43 @li mcf_getfmttype.test.sas
44 @li mp_init.sas
45
46 @todo "Custom Format Lookups" To enable site-specific formats, make
47 use of a set of SASJS_FMTLIST_(DATATYPE) global variables.
48
49**/
50
51%macro mcf_getfmttype(wrap=YES
52 ,lib=WORK
53 ,cat=SASJS
54 ,pkg=UTILS
55)/*/STORE SOURCE*/;
56%local i var cmpval found;
57
58%if %mcf_init(mcf_getfmttype)=1 %then %return;
59
60%if &wrap=YES %then %do;
61 proc fcmp outlib=&lib..&cat..&pkg;
62%end;
63
64function mcf_getfmttype(fmtnm $) $8;
65 if substr(fmtnm,1,1)='$' then return('CHAR');
66 else do;
67 /* extract NAME */
68 length fmt $32;
69 fmt=scan(fmtnm,1,'.');
70 do while (
71 substr(fmt,length(fmt),1) in ('1','2','3','4','5','6','7','8','9','0')
72 );
73 if length(fmt)=1 then fmt='W';
74 else fmt=substr(fmt,1,length(fmt)-1);
75 end;
76
77 /* apply lookups */
78 if cats(fmt) in ('DATETIME','B8601DN','B8601DN','B8601DT','B8601DT'
79 ,'B8601DZ','B8601DZ','DATEAMPM','DTDATE','DTMONYY','DTWKDATX','DTYEAR'
80 ,'DTYYQC','E8601DN','E8601DN','E8601DT','E8601DT','E8601DZ','E8601DZ'
81 ,'NLDATM') then return('DATETIME');
82 else if fmt in ('DATE','YYMMDD','B8601DA','B8601DA','DAY','DDMMYY'
83 ,'DDMMYYB','DDMMYYC','DDMMYYD','DDMMYYN','DDMMYYP','DDMMYYS','DDMMYYx'
84 ,'DOWNAME','E8601DA','E8601DA','JULDAY','JULIAN','MMDDYY','MMDDYYB'
85 ,'MMDDYYC','MMDDYYD','MMDDYYN','MMDDYYP','MMDDYYS','MMDDYYx','MMYY'
86 ,'MMYYC','MMYYD','MMYYN','MMYYP','MMYYS','MMYYx','MONNAME','MONTH'
87 ,'MONYY','PDJULG','PDJULI','QTR','QTRR','WEEKDATE','WEEKDATX','WEEKDAY'
88 ,'WEEKU','WEEKV','WEEKW','WORDDATE','WORDDATX','YEAR','YYMM','YYMMC'
89 ,'YYMMD','YYMMDDB','YYMMDDC','YYMMDDD','YYMMDDN','YYMMDDP','YYMMDDS'
90 ,'YYMMDDx','YYMMN','YYMMP','YYMMS','YYMMx','YYMON','YYQ','YYQC','YYQD'
91 ,'YYQN','YYQP','YYQR','YYQRC','YYQRD','YYQRN','YYQRP','YYQRS','YYQRx'
92 ,'YYQS','YYQx','YYQZ','NLDATE') then return('DATE');
93 else if fmt in ('TIME','B8601LZ','B8601LZ','B8601TM','B8601TM','B8601TZ'
94 ,'B8601TZ','E8601LZ','E8601LZ','E8601TM','E8601TM','E8601TZ','E8601TZ'
95 ,'HHMM','HOUR','MMSS','TIMEAMPM','TOD') then return('TIME');
96 else return('NUM');
97 end;
98endsub;
99
100%if &wrap=YES %then %do;
101 quit;
102%end;
103
104/* insert the CMPLIB if not already there */
105%let cmpval=%sysfunc(getoption(cmplib));
106%let found=0;
107%do i=1 %to %sysfunc(countw(&cmpval,%str( %(%))));
108 %let var=%scan(&cmpval,&i,%str( %(%)));
109 %if &var=&lib..&cat %then %let found=1;
110%end;
111%if &found=0 %then %do;
112 options insert=(CMPLIB=(&lib..&cat));
113%end;
114
115%mend mcf_getfmttype;