SAS I/O functions (ATTRN( ), ATTRC( ) , EXIST( ), OPEN( ), CLOSE( ) )
SAS provides number of I/O functions which are helpful to retrive metadata information about the sas dataset.
· EXIST(data_set_name) : checks for existence of dataset in the specified library. Returns 1 if data set exists else returns 0.
· OPEN(data_set_name ) : Returns Unique Dataset Identifier number of success else returns 0
· CLOSE(data_set_name) : Returns 0 on success else returns non-zero value.
· OPEN() and CLOSE() are important before doing other operations on SAS dataset. i.e. using functions which retrieve information about the dataset.
· Following functions are very useful to retrieve information about the data set
· ATTRN(datasetID, attributeName)
ATTRN() is used for retrieving numeric information about SAS dataset like number of records, number of variables, creation date etc.
Here datasetID = the unique ID for dataset which is returned by OPEN() function.
We can get information about following numeric attributes using ATTRN()
· NLOBS – It is second attribute for ATTRN( ) function, its gives Number of Logical observations which are not marked for deletion
· NOBS - Number of total observations
· NLOBSF – non deleted obervations that satisfy where clause or obs / Firstobs option.
· CRDDTE – creation time stamp of the dataset
· NVARS – number of variables in the dataset
· INSINDEX - whether or not the dataset is indexed
· ATTRC (datasetId, attributeName)
Similar to ATTRN( ), this function returns value of character attribute of desired data set. Various attribute names are
LABEL – Returns dataset label
SORTBY – variables name by which dataset is primarily sorted (if sorted at all)
MEM – data set name
LIB – current library for the dataset
· OPEN -> ATTRN -> ATTRC -> CLOSE
%let data_set = libname.data_set_name;
%let dsid = %sysfunc (open(&data_set));
%let nobs = %sysfunc(attrn(&dsid,nlobs));
%let create_date = %sysfunc(attrn(&dsid, crdte));
%let close_flag = %sysfunc(close(&dsid));