-
Notifications
You must be signed in to change notification settings - Fork 8
/
varexist.sas
63 lines (52 loc) · 2.19 KB
/
varexist.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
%macro varexist
/*----------------------------------------------------------------------
Check for the existence of a specified variable.
----------------------------------------------------------------------*/
(ds /* Data set name */
,var /* Variable name */
,info /* LEN = length of variable */
/* FMT = format of variable */
/* INFMT = informat of variable */
/* LABEL = label of variable */
/* NAME = name in case as stored in dataset */
/* TYPE = type of variable (N or C) */
/* Default is to return the variable number */
);
/*----------------------------------------------------------------------
This code was developed by HOFFMAN CONSULTING as part of a FREEWARE
macro tool set. Its use is restricted to current and former clients of
HOFFMAN CONSULTING as well as other professional colleagues. Questions
and suggestions may be sent to [email protected].
-----------------------------------------------------------------------
Usage:
%if %varexist(&data,NAME)
%then %put input data set contains variable NAME;
%put Variable &column in &data has type %varexist(&data,&column,type);
------------------------------------------------------------------------
Notes:
The macro calls resolves to 0 when either the data set does not exist
or the variable is not in the specified data set. Invalid values for
the INFO parameter returns a SAS ERROR message.
-----------------------------------------------------------------------
History:
12DEC98 TRHoffman Creation
28NOV99 TRHoffman Added info parameter (thanks Paulette Staum).
----------------------------------------------------------------------*/
%local dsid rc varnum;
%*----------------------------------------------------------------------
Use the SYSFUNC macro to execute the SCL OPEN, VARNUM,
other variable information and CLOSE functions.
-----------------------------------------------------------------------;
%let dsid = %sysfunc(open(&ds));
%if (&dsid) %then %do;
%let varnum = %sysfunc(varnum(&dsid,&var));
%if (&varnum) & %length(&info) %then
%sysfunc(var&info(&dsid,&varnum))
;
%else
&varnum
;
%let rc = %sysfunc(close(&dsid));
%end;
%else 0;
%mend varexist;