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**/
36
37%macro mp_getcols(ds, outds=work.cols);
38%local dropds;
39proc contents noprint data=&ds
40 out=_data_ (keep=name type length label varnum format:);
41run;
42%let dropds=&syslast;
43data &outds(keep=name type length varnum format label ddtype fmtname);
44 set &dropds(rename=(format=fmtname type=type2));
45 name=upcase(name);
46 if type2=2 then do;
47 length format $49.;
48 if fmtname='' then format=cats('$',length,'.');
49 else if formatl=0 then format=cats(fmtname,'.');
50 else format=cats(fmtname,formatl,'.');
51 type='C';
52 ddtype='CHARACTER';
53 end;
54 else do;
55 if fmtname='' then format=cats(length,'.');
56 else if formatl=0 then format=cats(fmtname,'.');
57 else if formatd=0 then format=cats(fmtname,formatl,'.');
58 else format=cats(fmtname,formatl,'.',formatd);
59 type='N';
60 if upcase(fmtname) in (%mf_dttmfmts(DATETIME)) then ddtype='DATETIME';
61 else if upcase(fmtname) in (%mf_dttmfmts(DATE)) then ddtype='DATE';
62 else if upcase(fmtname) in (%mf_dttmfmts(TIME)) then ddtype='TIME';
63 else ddtype='NUMERIC';
64 end;
65 if label='' then label=name;
66run;
67proc sql;
68drop table &dropds;
69%mend mp_getcols;