Sample 26003: Programatically determine if a variable exists in a data set
Use the OPEN and VARNUM functions to determine if a variable exists in a SAS data set. The OPEN function opens a SAS data set and returns
a data set identifier. The VARNUM function returns the variable's position in the data set. If the variable does not exist then VARNUM returns a 0.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
/* Create sample data */
data test;
input fruit $ count;
datalines;
apple 12
banana 4
coconut 5
date 7
eggs 9
fig 5
;
/* Specify the data set to open in the OPEN function. Specify the */
/* variable to be located in the second parameter of the VARNUM */
/* function. If the variable does not exist, the value of check */
/* will be zero. */
data _null_;
dsid=open('test');
check=varnum(dsid,'count');
if check=0 then put 'Variable does not exist';
else put 'Variable is located in column ' check +(-1) '.';
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
/* Partial LOG output */
311 data _null_;
312 dsid=open('test');
313 check=varnum(dsid,'count');
314 if check=0 then put 'Variable does not exist';
315 else put 'Variable is located in column ' check +(-1) '.';
316 run;
Variable is located in column 2.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
Use the OPEN and VARNUM functions to determine if a variable exists in a data set.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step SAS Reference ==> Functions ==> SAS File I/O
|
| Date Modified: | 2006-01-10 03:02:52 |
| Date Created: | 2006-01-09 11:37:02 |
Operating System and Release Information
| Product Family | Product | Host | Starting Release | Ending Release |
| SAS System | Base SAS | All | 8.2 TS2M0 | n/a |