4 Methods to create macro variables from SAS dataset

How many ways we can create macro variables in SAS?

Macro variables in SAS are used extensively for reusing codes. You can create a multi-usable program and control the analysis of variables by updating the values of macro variables.

LET statement can create a few macro variables that work exceptionally well. However, the data step with CALL SYMPUT or INTO Statement in PROC SQL provides a better method to automate the creation of macro variables.

Data step with CALL SYMPUT or INTO Statement in PROC SQL provides a better method to automate the creation of macro variables.

1. %LET statement for creating a single macro variable

The %LET statement is one of the easiest methods to create a macro variable in SAS and is equivalent to the DATA step’s assignment statement.

%LET statement is followed by the macro variable name, an equal sign (=), and then the text value assigned to the macro variable.

Syntax:

%LET <macro-variable-name> = <Macro-variable-value>;

Example of let Statement to create a macro Variable:

%LET Name = Smith;

Quotation marks are not used with the LET Statement when creating macro variables since macro variables are neither character nor numeric and are always stored as plain text. 

In this example, the %let statement creates the macro variable & name.

The value of ‘Smith‘ can be used anywhere in your program.

However, if you have more than a few macro variables to create, typing the %let statements can be tedious and error-prone.

2. Using Call Symput to create macro variables in SAS

You cannot use the %let statement to convert macro SAS dataset variables to macro variables. However, you cannot use the %let statement to convert SAS dataset variables to macro variables. This section explains how to create multiple macro variables in SAS using the CALL SYMPUT routine.

Let us say you want to store the value for the name variable from the SASHELP.CLASS dataset in the fname macro variable.

If you run the following code:

data new_class;
set sashelp.class;
%let FName = Name;
run;

You will find that the macro variable & name will resolve to ‘Name’ as the value and not the actual value in the data set.

That is because %LET statements are executed before the compiled data step.

To overcome this problem, we will need to use the data step to place information onto the macro variable symbol tables.

We can do this with SYMPUT, a DATA step call routine, and not a macro language statement.

Syntax:

CALL SYMPUT(macro-variable, value);

Call Symput Example in SAS

data new_class;
set sashelp.class;
call symput('FName',Name);
run;

In the above example, the CALL SYMPUT statement creates the macro variable &Fname containing the variable name. 

However, only the last value of the variable name is assigned to the macro variable.

To overcome this, you can include a suffix to place all the values of the name into a macro variable.

data new_class;
set sashelp.class;
suffix=put(_n_,5.);
call symput(cats('FName',suffix),Name);
run;

The suffix variable counts the observation (_n_) and appends count onto the macro variable Fname to create unique Macro variables for each name.

The resultant macro variables &Fname1 will store the first name value, the second value is stored in the macro variable &Fname2 and so on.

To retrieve any value of the Name variable, you can use the below statement and replace the number with the observation number.

The statement below resolves to ‘Carol‘, the fourth observation in the dataset.

%put &Fname4;

3. Creating Macros using PROC SQL

You can also automate the creation of a macro variable using PROC SQL.

Placing a single value in a macro variable

There are situations where you may require to count the number of observations in a table. 

How to store the count of observations of a SAS dataset in a macro variable?

In the example below, we will use the CLASS dataset from the SASHELP library. This dataset contains 19 observations and five columns.

The easiest method to store the observation count in a macro variable is to use count(*) in Proc SQL with the INTO keyword. It returns all rows (missing plus non-missing rows) in a dataset.

There are situations where you may require to count the number of observations in a table. 

[ You might also like: 8 Ways to count the number of observations in a SAS dataset and pass it into a macro variable ]

proc sql noprint;
select count(*) into :nobs from sashelp.class;
quit;

The macro variables NOBS is created using the INTO clause.

SELECT Statement with the colon(:) tells SAS to store the result of the count function into a macro variable.

You can also create macro variables for each value, as we created earlier in the data step as in the below example.

proc sql noprint;
select count(*) into :nobs 
from sashelp.class;
select Name into :Name1-:Name%left(&nobs) 
from sashelp.class;
quit;

Unlike the data step method, you must know there are 19 suffixes for the macro name, or you could first calculate the number of Observations and store them in a separate macro variable.

In the example below, &name and &age macro variables are created, each containing a comma-separated list of values.

proc sql outobs=5 noprint;
select name,age into :names separated by ',', :age separated by '-' 
from sashelp.class;
quit;
%put names are &names;
%put ages are &age;
%put number of obs &sqlobs;

The OUTOBS=5 option limits the number of observations for processing to 5.

The variables’ values, Name and Age, are written INTO macro variables, and it contains comma-separated values of the name and age variable, respectively.

The SEPARATED BY clause appends values to the comma-separated list.

The last comma is needed to separate the two macro variables (names and age).

The list of the age will be written to &age.

&SQLOBS is an automated macro variable and contains the number of rows processed to create the macro variable list or range. In this case, it contains the number of rows as 5.

%put names are &names; 
names are Alfred,Alice,Barbara,Carol,Henry74 

%put ages are &age; 
ages are 14,13,13,14,1475 

%put number of obs 
number of obs 5

4. Creating Macro using CALL SET

You can use the CALL SET statement to replace the SET statement in a SAS data step.

%macro createMacro();
%let dsid=%sysfunc(open(sashelp.class));
%let nobs=%sysfunc(attrn(&dsid, nobs));
%syscall set(dsid);
%do i=1 %to &nobs;
%let rc=%sysfunc(fetchobs(&dsid, &i));
%put &name &age &weight;
%end;
%let dsid=%sysfunc(close(&dsid));
%mend;

%createMacro();

The %SYSCALL SET” is the macro version of “CALL SET”, which creates a macro variable corresponding to each of the variables in the data set.

FETCHOBS is a data access function used to read a specified observation from a SAS data set. 

When an observation is read using FETCHOBS, each of these macro variables is assigned with the value of the corresponding data set variable.

Now, you have all the dataset variables replaced with the macro variables without any need for PROC SQL or CALL SYMPUT.

Additionally, the macro variables need no suffix, so you do not need a macro variable for each observation. It can significantly reduce the number of system resources.

Conclusion

Now, you know the four methods to create macro variables in SAS. Nevertheless, creating macro variables depends on the programmer’s preferences.
%LET is adequate for creating a single macro variable.

If you want to create many macro variables, consider using the CALL SYMPUT or PROC SQL methods.

However, if the requirement is to create many macro variables to process each observation in the data step, then CALL SET will lead to concise and efficient coding.

If you found this post helpful, make sure you share and comment.

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro

Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.