File Manipulation in SAS Macro
This may be easier than you thought - though you will find yourself using %SYSFUNC rather a lot. In this first example we gather information about the attributes of a dataset:
We use the EXIST function to check that the data set exists. We then use the OPEN function to open it, and get a "data set ID" for it, which will be required for other file-related functions. The ATTRN function is then used to get information about it. Finally we use the CLOSE function to close the data set. The output is:
The ATTRN and ATTRC functions can be used to get a lot of other information about a dataset - for example, whether it is indexed, what it is sorted by, and what engine it uses.
Now we gather information about a particular variable:
Here we have to use the VARNUM function to find the number of our variable within the dataset. This can then be used as a parameter to functions such as VTYPE and VLEN which yield information about the variable. The output is:
Finally, we can access data from particular observations of the dataset:
The important step here is the use of "%SYSCALL SET" - the macro version of "CALL SET" - which creates a macro variable corresponding to each variable of the data set. (NB the "DSID" parameter here is not introduced by an ampersand.) When we read in an observation using FETCHOBS, each of these macro variables is populated with the value of the corresponding data set variable. In this example, we display two of them. The output is: