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