Amadeus cookies policy - you'll see this message only once.

Amadeus use cookies on this website. They help us to know a little bit about you and how you use our website, which improves the browsing experience and marketing - both for you and for others. They are stored locally on your computer or mobile device. To accept cookies, continue browsing as normal. Or, go to the privacy policy for more information.

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:

File manipulation in SAS Macro image 1

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:

File manipulation in SAS Macro image 2

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:

File manipulation in SAS Macro image 3

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:

File manipulation in SAS Macro image 4

Finally, we can access data from particular observations of the dataset:

File manipulation in SAS Macro image 5

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:

File manipulation in SAS Macro image 6