Macros for SAS Application Developers
https://github.com/sasjs/core
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(wrap=YES, insert_cmplib=YES)
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= (NO) Choose YES to add 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  @param [out] insert_cmplib= DEPRECATED - The CMPLIB option is checked and
39  values inserted only if needed.
40 
41  <h4> SAS Macros </h4>
42  @li mcf_init.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=NO
54  ,insert_cmplib=DEPRECATED
55  ,lib=WORK
56  ,cat=SASJS
57  ,pkg=UTILS
58 )/*/STORE SOURCE*/;
59 %local i var cmpval found;
60 
61 %if %mcf_init(mcf_getfmttype)=1 %then %return;
62 
63 %if &wrap=YES %then %do;
64  proc fcmp outlib=&lib..&cat..&pkg;
65 %end;
66 
67 function mcf_getfmttype(fmtnm $) $8;
68  if substr(fmtnm,1,1)='$' then return('CHAR');
69  else do;
70  /* extract NAME */
71  length fmt $32;
72  fmt=scan(fmtnm,1,'.');
73  do while (
74  substr(fmt,length(fmt),1) in ('1','2','3','4','5','6','7','8','9','0')
75  );
76  if length(fmt)=1 then fmt='W';
77  else fmt=substr(fmt,1,length(fmt)-1);
78  end;
79 
80  /* apply lookups */
81  if cats(fmt) in ('DATETIME','B8601DN','B8601DN','B8601DT','B8601DT'
82  ,'B8601DZ','B8601DZ','DATEAMPM','DTDATE','DTMONYY','DTWKDATX','DTYEAR'
83  ,'DTYYQC','E8601DN','E8601DN','E8601DT','E8601DT','E8601DZ','E8601DZ')
84  then return('DATETIME');
85  else if fmt in ('DATE','YYMMDD','B8601DA','B8601DA','DAY','DDMMYY'
86  ,'DDMMYYB','DDMMYYC','DDMMYYD','DDMMYYN','DDMMYYP','DDMMYYS','DDMMYYx'
87  ,'DOWNAME','E8601DA','E8601DA','JULDAY','JULIAN','MMDDYY','MMDDYYB'
88  ,'MMDDYYC','MMDDYYD','MMDDYYN','MMDDYYP','MMDDYYS','MMDDYYx','MMYY'
89  ,'MMYYC','MMYYD','MMYYN','MMYYP','MMYYS','MMYYx','MONNAME','MONTH'
90  ,'MONYY','PDJULG','PDJULI','QTR','QTRR','WEEKDATE','WEEKDATX','WEEKDAY'
91  ,'WEEKU','WEEKV','WEEKW','WORDDATE','WORDDATX','YEAR','YYMM','YYMMC'
92  ,'YYMMD','YYMMDDB','YYMMDDC','YYMMDDD','YYMMDDN','YYMMDDP','YYMMDDS'
93  ,'YYMMDDx','YYMMN','YYMMP','YYMMS','YYMMx','YYMON','YYQ','YYQC','YYQD'
94  ,'YYQN','YYQP','YYQR','YYQRC','YYQRD','YYQRN','YYQRP','YYQRS','YYQRx'
95  ,'YYQS','YYQx','YYQZ') then return('DATE');
96  else if fmt in ('TIME','B8601LZ','B8601LZ','B8601TM','B8601TM','B8601TZ'
97  ,'B8601TZ','E8601LZ','E8601LZ','E8601TM','E8601TM','E8601TZ','E8601TZ'
98  ,'HHMM','HOUR','MMSS','TIMEAMPM','TOD') then return('TIME');
99  else return('NUM');
100  end;
101 endsub;
102 
103 %if &wrap=YES %then %do;
104  quit;
105 %end;
106 
107 /* insert the CMPLIB if not already there */
108 %let cmpval=%sysfunc(getoption(cmplib));
109 %let found=0;
110 %do i=1 %to %sysfunc(countw(&cmpval,%str( %(%))));
111  %let var=%scan(&cmpval,&i,%str( %(%)));
112  %if &var=&lib..&cat %then %let found=1;
113 %end;
114 %if &found=0 %then %do;
115  options insert=(CMPLIB=(&lib..&cat));
116 %end;
117 
118 %mend mcf_getfmttype;