In SAS, the _null_ keyword in a SAS Data Step allows us to create a SAS dataset with no records and variables. With _null_, surprisingly, we are able to many things in our SAS code.
In this post, you will learn the many benefits of using _null_ in your SAS code.
Using the SAS _null_ Keyword in Your SAS Code
The SAS keyword _null_ creates a SAS dataset with no observations and no variables.
You can use _null_ in a data step for both the input and output dataset.
If it is specified as the input dataset, then you will create a dataset with no observations and no variables as shown below.
data out;
set _null_;
run;
Using _null_ in this case can be useful if you want to initialize an empty dataset and you will add records to that dataset in a SAS Macro loop, for example.
data out;
set _null_;
run;
%macro example_loop;
%do i = 0 %to 10;
/* some calculations */
data out other_dataset;
set out;
run;
%end;
%mend;
Otherwise, if you are using _null_ as the output dataset, then nothing will be created after the data step.
Even though this may seem useless, there are actually many ways you can use data _null_ to perform different tasks in SAS.
The Many Uses of data _null_ in SAS
There are a number of ways you can use data _null_ in a SAS data step.
One very common way you can use data _null_ is to create macro variables.
You can use the symput() and symputx() subroutines to create SAS macro variables from values in a SAS data set.
For example, if you want to get a value from a specific variable and create a macro variable from that value, you can do so as shown below.
data _null_;
set summary_dataset;
if name = "Sally" then call symputx("SallyVar", variable);
run;
You can also use data _null_ to put different values to the log, such as the value of a variable after a calculation or information about the dataset.
For example, if you want to do a specific calculation and put that value to the log, then you can use data _null_
data _null_;
x = 5 * 10;
put x;
run;
The majority of cases you will use data _null_ for fall into these two categories – creating macro variables or putting information to the log.
Hopefully this article has been useful for you to learn how to use data _null_ in your SAS code.