-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathvarinfo.sas
97 lines (75 loc) · 3.38 KB
/
varinfo.sas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
%macro VarInfo(ds,var,infotype) / des='Output info on variable';
/********************************************************************************
BEGIN MACRO HEADER
********************************************************************************
Name: VarInfo
Author: Chris Swenson
Created: 2010-05-18
Purpose: Output info on a variable
Arguments: ds - data set of variable in question
var - variable to obtain information about
infotype - type of information to obtain
Usage: See the SAS documentation on the ATTRC and ATTRN functions for
valid values for the infotype argument
Revisions
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Date Author Comments
¯¯¯¯¯¯¯¯¯¯ ¯¯¯¯¯¯ ¯¯¯¯¯¯¯¯
YYYY-MM-DD III Please use this format and insert new entries above
********************************************************************************
END MACRO HEADER
********************************************************************************/
/* Check if the table exists */
%if "&ds"="" %then %do;
%put %str(E)RROR: The data set argument is blank.;
%return;
%end;
%if %eval(%sysfunc(exist(&ds, %str(DATA))) + %sysfunc(exist(&ds, %str(VIEW))))=0 %then %do;
%put %str(W)ARNING: %sysfunc(compbl(The &ds data set does not exist)).;
%return;
%end;
/* Check the var argument */
%if "&var"="" %then %do;
%put %str(E)RROR: The variable argument is blank.;
%return;
%end;
/* Check infotype argument */
%let infotype=%upcase(&infotype);
%if "&infotype"="" %then %do;
%put %str(E)RROR: The infotype argument is blank.;
%return;
%end;
%if %index(*FORMAT*INFORMAT*LABEL*LENGTH*TYPE*,*&INFOTYPE*)=0 %then %do;
%put %str(E)RROR: Infotype argument is %str(i)nvalid. Use one of the following: format, informat, label, length, or type.;
%return;
%end;
/* Manage scope */
%local arg dsid vid rc;
/* Translate infotype argument to function */
%if &infotype=FORMAT %then %let arg=varfmt;
%else %if &infotype=INFORMAT %then %let arg=varinfmt;
%else %if &infotype=LABEL %then %let arg=varlabel;
%else %if &infotype=LENGTH %then %let arg=varlen;
%else %if &infotype=TYPE %then %let arg=vartype;
/* Open data set */
%let dsid=%sysfunc(open(&ds));
/* Obtain variable number */
%let vid=%sysfunc(varnum(&dsid, %upcase(&var)));
%if &vid>0 %then %do;
/* Obtain variable info */
%let info=%sysfunc(&arg(&dsid, &vid));
%if &infotype=FORMAT and "&info"="" %then %do;
%if %sysfunc(vartype(&dsid, &vid))=C %then %let info=$%sysfunc(varlen(&dsid, &vid)).;
%else %if %sysfunc(vartype(&dsid, &vid))=N %then %let info=%sysfunc(varlen(&dsid, &vid)).;
%end;
%end;
%else %do;
%let rc=%sysfunc(close(&dsid));
%put %str(W)ARNING: Variable &var does not exist on data set &ds..;
%return;
%end;
/* Close data set */
%let rc=%sysfunc(close(&dsid));
/* Output type */
&info
%mend VarInfo;