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
11 Example usage:
12
13 %mp_getcols(sashelp.airline,outds=work.myds)
14
15 @param [in] ds The dataset from which to obtain column metadata
16 @param [out] outds= (work.cols) The output dataset to create. Sample data:
17|NAME:$32.|LENGTH:best.|VARNUM:best.|LABEL:$256.|FMTNAME:$32.|FORMAT:$49.|TYPE:$1.|DDTYPE:$9.|
18|---|---|---|---|---|---|---|---|
19|`AIR `|`8 `|`2 `|`international airline travel (thousands) `|` `|`8. `|`N `|`NUMERIC `|
20|`DATE `|`8 `|`1 `|`DATE `|`MONYY `|`MONYY. `|`N `|`DATE `|
21|`REGION `|`3 `|`3 `|`REGION `|` `|`$3. `|`C `|`CHARACTER `|
22
23
24 <h4> Related Macros </h4>
25 @li mf_getvarlist.sas
26 @li mm_getcols.sas
27
28 @version 9.2
29 @author Allan Bowe
30
31**/
32
33%macro mp_getcols(ds, outds=work.cols);
34%local dropds;
35proc contents noprint data=&ds
36 out=_data_ (keep=name type length label varnum format:);
37run;
38%let dropds=&syslast;
39data &outds(keep=name type length varnum format label ddtype fmtname);
40 set &dropds(rename=(format=fmtname type=type2));
41 name=upcase(name);
42 if type2=2 then do;
43 length format $49.;
44 if fmtname='' then format=cats('$',length,'.');
45 else if formatl=0 then format=cats(fmtname,'.');
46 else format=cats(fmtname,formatl,'.');
47 type='C';
48 ddtype='CHARACTER';
49 end;
50 else do;
51 if fmtname='' then format=cats(length,'.');
52 else if formatl=0 then format=cats(fmtname,'.');
53 else if formatd=0 then format=cats(fmtname,formatl,'.');
54 else format=cats(fmtname,formatl,'.',formatd);
55 type='N';
56 if format=:'DATETIME' or format=:'E8601DT' then ddtype='DATETIME';
57 else if format=:'DATE' or format=:'DDMMYY' or format=:'MMDDYY'
58 or format=:'YYMMDD' or format=:'E8601DA' or format=:'B8601DA'
59 or format=:'MONYY'
60 then ddtype='DATE';
61 else if format=:'TIME' then ddtype='TIME';
62 else ddtype='NUMERIC';
63 end;
64 if label='' then label=name;
65run;
66proc sql;
67drop table &dropds;
68%mend mp_getcols;