Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_getcols.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Creates a dataset with column metadata.
4 @details This macro takes the `proc contents` output and "tidies it up" in the
5 following ways:
6
7 @li Blank labels are filled in with column names
8 @li Formats are reconstructed with default values
9 @li Types such as DATE / TIME / DATETIME are inferred from the formats,
10 using mf_dttmfmts.sas so every caller shares one set of format lists
11
12 Example usage:
13
14 %mp_getcols(sashelp.airline,outds=work.myds)
15
16 @param [in] ds The dataset from which to obtain column metadata
17 @param [out] outds= (work.cols) The output dataset to create. Sample data:
18|NAME:$32.|LENGTH:best.|VARNUM:best.|LABEL:$256.|FMTNAME:$32.|FORMAT:$49.|TYPE:$1.|DDTYPE:$9.|
19|---|---|---|---|---|---|---|---|
20|`AIR `|`8 `|`2 `|`international airline travel (thousands) `|` `|`8. `|`N `|`NUMERIC `|
21|`DATE `|`8 `|`1 `|`DATE `|`MONYY `|`MONYY. `|`N `|`DATE `|
22|`REGION `|`3 `|`3 `|`REGION `|` `|`$3. `|`C `|`CHARACTER `|
23
24
25 <h4> SAS Macros </h4>
26 @li mf_dttmfmts.sas
27
28 <h4> Related Macros </h4>
29 @li mf_getvarlist.sas
30 @li mm_getcols.sas
31
32 @version 9.2
33 @author Allan Bowe
34
35 @sasjslint {"maxLineLength": 94}
36**/
37
38%macro mp_getcols(ds, outds=work.cols);
39%local dropds;
40proc contents noprint data=&ds
41 out=_data_ (keep=name type length label varnum format:);
42run;
43%let dropds=&syslast;
44data &outds(keep=name type length varnum format label ddtype fmtname);
45 set &dropds(rename=(format=fmtname type=type2));
46 name=upcase(name);
47 if type2=2 then do;
48 length format $49.;
49 if fmtname='' then format=cats('$',length,'.');
50 else if formatl=0 then format=cats(fmtname,'.');
51 else format=cats(fmtname,formatl,'.');
52 type='C';
53 ddtype='CHARACTER';
54 end;
55 else do;
56 if fmtname='' then format=cats(length,'.');
57 else if formatl=0 then format=cats(fmtname,'.');
58 else if formatd=0 then format=cats(fmtname,formatl,'.');
59 else format=cats(fmtname,formatl,'.',formatd);
60 type='N';
61 if upcase(fmtname) in (%mf_dttmfmts(DATETIME)) then ddtype='DATETIME';
62 else if upcase(fmtname) in (%mf_dttmfmts(DATE)) then ddtype='DATE';
63 else if upcase(fmtname) in (%mf_dttmfmts(TIME)) then ddtype='TIME';
64 else ddtype='NUMERIC';
65 end;
66 if label='' then label=name;
67run;
68proc sql;
69drop table &dropds;
70%mend mp_getcols;