User:TobyDunn/BlogEntry: 2007 May 06 18:29:24 EDT
From sasCommunity
[edit] Part 1 of 3
This is Part 1 of a 3 part series on advanced Macro programming. Most programmers who use Macros only think of a Macro as a means to reproduce Data Step or Procedure code with minor tweaks. This mind set for beginning programmers is a good thing, however as a programmers ability increases there arises a need to have a macro read a Data Set, read a external file, or write to an external file entirely in pure Macro code.
Example 1:
%Macro GetValues( DataIn = , Variable = ) ;
%Local DSID GetNextObs GetVarNum Close I ;
%Let DSID = %SysFunc( Open( &DataIn , I ) ) ;
%Do I = 1 %To %SysFunc( Attrn( &DSID , NLOBS ) ) ;
%Let GetNextObs = %SysFunc( FetchObs( &DSID , &I ) ) ;
%Let GetVarNum = %SysFunc( VarNum( &DSID , &Variable ) ) ;
%SysFunc( GetVarC( &DSID , &GetVarNum ) )
%End ;
%Let Close = %SysFunc( Close( &DSID ) ) ;
%Mend GetValues ;
%Put %GetValues( DataIn = SASHELP.CLASS , Variable = Name ) ;
There are a few things that one needs to know to read a data set with pure macro code. First is how to open and close a Data Set. For this we use the Open and Close functions. Since these are not Macro functions we need away to get the Macro facility to interface with them, so we see %Sysfunc which allows us to do just that. It is important to note that you cannot forget to Close your Data Set. By default all Data Sets are closed at the end of a Data Step, however you aren’t in that environment so it is left to, you, the programmer to close the Data Set. If you do forget to Close your Data Set you will not be able to use the Data Set again until Close it or restart your SAS session. This is due to the fact that when you open a Data Set in this manner you lock it down until you specifically release it for further use.
Next we use the Attrn function to retrieve the number of observations in the Data Set. In a normal Data Step there is an implicit loop which cycles through the observations for you. Since we don’t have that implicit loop we need to construct our own loop and thus need specify how many times we want to loop through the Data Set.
In a Data Step we have the Program Data Vector which is were the data is brought into and manipulated while the Data Step is running before writing the data out. We also need such a space and to do this we use FetchObs which gets the observation specified in the second argument and brings its value(s) into what is called the Data Set Data Vector.
Now that we have an observations value in a place we can retrieve it we need to specify which variable’s value we want to retrieve. For this we use the VarNum function, which when passed the Data Set Id and variable name will return the variables position in the Data Set.
Finally we can retrieve the variables value with GetVarC which gets character variables values from a Data Set and returns said value to a Macro variable. If one wanted to retrieve a numeric variables value then use GetVarN.
Last but not least after all observations are processed we close the Data Set with the Close function.
In our next installment we will see how to read all the files in a directory with pure Macro code.
