Lesson 2, Part 4: Using DATA _Null_¶

6 ways to use the _NULL_ data set in SAS by Rick Wicklin

In [ ]:
ods html close;
options nocenter  nonotes  nonumber nodate nosource;
data _Null_;
* You have been given the following two pieces of information;
height = 69.0;
weight = 112.5 ;

* Calculate the body mass index (BMI) based on height (in inches) and weight (in pounds);
BMI = round((weight / (height*height) ) * 703, .1);

* Convert height (in inches) to height (in meters);
    height_in_meters = round(height * 0.0254, .01);
 put   height=F5.1  weight= bmi=  height_in_meters=;
 
RUN;

Displaying Characteristics of SAS Data Sets by Using DATA _NULL_¶

Source: Rick Wicklin¶

In [3]:
data _NULL_;
set Sashelp.Class;
array char[*] $ _CHAR_;
array num[*] _NUMERIC_;
nCharVar  = dim(char);
nNumerVar = dim(num);
put "Sashelp.Class: " nCharVar= nNumerVar= ;
stop;   /* stop processing after first observation */
run;
The SAS System

Sashelp.Class: nCharVar=2 nNumerVar=3

The SAS System

E3969440A681A2408885998500000005

Creating macro variables from a value in a SAS data set¶

In [11]:
ods html close;
options nocenter nodate nonumber nosource;
proc means data=sashelp.class mean maxdec=1 noprint;
 var weight;
 output out=stats mean=average_wgt;
run;
The SAS System

E3969440A681A2408885998500000013

This CALL SYMPUTX routine has two parameters separated by a comma inside parentheses.¶

  • The first parameter, a constant enclosed in quote marks (OVERALL_MEAN), is the name of the macro variable being created.

  • The second parameter is the value of the DATA step variable AVERAGE_WGT being assigned to the macro variable.

  • The code creates 1 macro variable, and its value is a character string. The macro variable resides in the GLOBAL symbol table.

The %PUT statement¶

  • writes text strings and values of the macro variables to the SAS log, starting in column one
  • writes a blank line if text is not specified
  • does not require quotation marks around text
  • is valid in open code
  • can appear
    • before the DATA step
    • after the DATA step
    • in the middle of the DATA step
In [14]:
ods html close;
options nocenter nodate nonumber nosource;
 data _null_;
  set stats;
  call symputx('OVERALL_MEAN', average_wgt);
 run; 
 %put _user_;
The SAS System

GLOBAL OVERALL_MEAN 100.02631579
The SAS System

E3969440A681A2408885998500000016

Explanation of the SAS Code Below¶

  • The macro variable is referenced within double quotation marks in the TITLE statement.

  • You must use quotation marks to enable macro variable resolution. Single quotation marks prevent macro varaiable resolution.

  • SYSDATE9 is an automatic macro variable, set at SAS invocation, and always available.

In [ ]:
*Ex_Motivation_for_macro_variables (Part 4);
  *The macro variable value can be retrieved in a data step; 
options nocenter nodate nonumber nosource;
 data test2;
  set SASHELP.class;
  weight_ratio=weight/"&OVERALL_MEAN";
 run;
 title "Macro variable retrived in the DATA step -  executed on &sysdate9";
 proc print data=test2 (obs=5); run;
 title;

Explanation of the SAS Code Below¶

  • The PUT statement writes to the LOG or to an External File with a FILE statement, but the PUTLOG statement always writes to the LOG.

  • The keyword _NULL_ on the DATA statement is used to execute the data step without creating a data set.

  • The PUT statement is used to create output records to the LOG window. The special SAS name list _ALL_ refers to all variables on the data step and program data vector including _N_ and _ERROR_.

In [19]:
ods html close;
options nosource nodate nonumber nonotes;
ODS EXCLUDE ALL;
data _null_;
  set sashelp.class(obs=2);
  put _all_;
run;
ODS EXCLUDE NONE;
The SAS System

Name=Alfred Sex=M Age=14 Height=69 Weight=112.5 _ERROR_=0 _N_=1
Name=Alice Sex=F Age=13 Height=56.5 Weight=84 _ERROR_=0 _N_=2
The SAS System

E3969440A681A2408885998500000021

PUT Statement¶

  • In the PUT statement, the variable list argument is _ALL_; the FORMAT argument is the equal sign so that the variable name preceded its value.
In [16]:
ods html close;
options nonotes nodate nonumber;
data _null_;
  set sashelp.class(obs=2);
  put (_all_)(=);
run;
The SAS System

Name=Alfred Sex=M Age=14 Height=69 Weight=112.5
Name=Alice Sex=F Age=13 Height=56.5 Weight=84
The SAS System

E3969440A681A2408885998500000018

PUT Statement¶

  • The PUT statement is used to tell SAS to write each of the variables in the data step and program data vector (except _N_ and _ERROR_) that must precede its value.

  • An additional format argument / is used so that each variable and its value separated by an equal sign is output to a separate line.

In [ ]:
* Put each value on a new line;
data _null_;
  set sashelp.class(obs=2);
  put (_all_)(=/);
run;
In [ ]:
/* Put each value on a new line and apply 
a common format to all numeric variables*/
data _null_;
  set sashelp.class(obs=2);
  put (_all_)(=/12.2);
run;
In [24]:
ods html close;
options nocenter nodate nonumber nonotes nosource; 
data _null_;
  input;
  if _N_ =1 then putlog 'Address of the Stat Department:';
   putlog _INFILE_ ;
 datalines4;
Department of Statistics
Columbian College of Arts & Sciences
Rome Hall
801 22nd St NW, 7th Floor
Washington, DC, 20052
Phone: 202-994-6356 | Fax: 202-994-6917
;;;;
The SAS System

Address of the Stat Department:
Department of Statistics                                                        
Columbian College of Arts & Sciences                                            
Rome Hall                                                                       
801 22nd St NW, 7th Floor                                                       
Washington, DC, 20052                                                           
Phone: 202-994-6356 | Fax: 202-994-6917                                         
The SAS System

E3969440A681A2408885998500000026

Explanation of the SAS Code Below¶

  • During the first iteration, the variable names are printed each starting with the column position specified, to the LOG window by default.

  • In the the FORMATTED PUT statement, formats are specified as format-list arguments. For example, a character format $20. is applied to the variable NAME.

  • A character format $2. is applied to the variable SEX.

  • A numeric format 3. is applied to the variable AGE. Another numeric format 8.2 is applied to the variables HEIGHT and WEIGHT.

In [26]:
ods html close;
options nocenter nodate nonumber nonotes nosource; 
data _null_;
  set sashelp.class(obs=2);
  if _n_=1 then put @1 'NAME' @19 'SEX' @23 'AGE' 
                    @30 'HEIGHT' @38 'WEIGHT';
  put (_all_)(1*$20.,1*$2.,1*3.,2*8.2);
run;
The SAS System

NAME              SEX AGE    HEIGHT  WEIGHT
Alfred              M  14   69.00  112.50
Alice               F  13   56.50   84.00
The SAS System

E3969440A681A2408885998500000028

Explanation for the SAS Code Below¶

  • List values as a table.

  • Apply formats to groups of variables.

  • Route output to the standard SAS output window.

The PUT statement creates the tabular output to the OUTPUT window, not the LOG window.

In [8]:
ods html close;
options nodate nonumber;
title;
data _null_;
  set sashelp.class(obs=2);
  file print;
  if _n_=1 then put @1 'NAME' @19 'SEX' @23 'AGE' 
                    @30 'HEIGHT' @38 'WEIGHT';
  put (_all_)(1*$20.,1*$2.,1*3.,2*8.2);
run;
SAS Output

NAME              SEX AGE    HEIGHT  WEIGHT                                                                                         

Alfred              M  14   69.00  112.50                                                                                           

Alice               F  13   56.50   84.00                                                                                           

Explanation of the SAS Code Below¶

  • List values as a table.

  • The PUT statement creates the tabular output to a file that is specified in the FILE statement, not to the LOG or OUTPUT window.

In [28]:
ods html close;
options nodate nonumber;
Data _Null_;
 file 'C:\Explore\SAS\Lesson2\Lesson2Data\class2.csv';
 set sashelp.class END=last;
 put (_all_) (',');
if last then putlog "User's NOTE: Writing to the File is completed";
run;
                                                           The SAS System

User's NOTE: Writing to the File is completed
                                                           The SAS System

E3969440A681A2408885998500000030

Explanation of the SAS Code Below¶

  • List values as a table.

  • Create a header for the tabular data file.

  • Apply formats to groups of variables.

  • The PUT statement creates the tabular output to a file that is specified in the FILE statement to OUTPUT window.

In [35]:
ods html close;
options nodate nonumber;
data _null_;
  set sashelp.class(obs=2);
  file print;
  if _n_=1 then put @1 'NAME' @19 'SEX' @23 'AGE' 
                    @30 'HEIGHT' @38 'WEIGHT';
  put (_all_)(1*$20.,1*$2.,1*3.,2*8.2);
run;
SAS Output

The SAS System


NAME              SEX AGE    HEIGHT  WEIGHT                                                                                         

Alfred              M  14   69.00  112.50                                                                                           

Alice               F  13   56.50   84.00                                                                                           

Explanation of the SAS Code Below¶

  • List values as a table and apply formats to groups of variables.

  • In the SET statement, the END= option defines a temporary variable whose value is 1 when the DATA step is processing the last observation. At all other times, the value of variable is 0. Although the DATA step can use the END= variable, SAS does not add it to the resulting data set”. [SAS Documentation]

  • The FILE statement creates a regular raw data file.

  • During the first iteration, the variable names are printed each starting with the column position specified, to the LOG window by default.

  • In the FORMATTED PUT statement, formats specified as format-list arguments. For example, a character format $20. is applied to the variable NAME.

  • A character format $2. is applied to the variable SEX.

  • A numeric format 3. is applied to the variable AGE. Another numeric format 8.2 is applied to the variables HEIGHT and WEIGHT.

  • The PUTLOG statement is used to write an informational message to the LOG. Note that we have preceded a message text with User’s Note to better identify the output in the log.

In [8]:
ods html close;
options nodate nonumber nosource nonotes;
title;
data _null_;
  set sashelp.class(obs=2) END=last;
  file 'C:\Explore\SAS\Lesson2\Lesson2Data\class_data2.txt';
  if _n_=1 then put @1 'NAME' @19 'SEX' @2 'AGE' 
                    @30 'HEIGHT' @38 'WEIGHT';
  put (_all_)(1*$20.,1*$2.,1*3.,2*8.2);
  if last then putlog "User's NOTE: Writing to the File is completed";
run;
                                                           The SAS System

User's NOTE: Writing to the File is completed
                                                           The SAS System

E3969440A681A2408885998500000010
In [6]:
%showLog
                                                           The SAS System

User's NOTE: Writing to the File is completed

                                                           The SAS System

E3969440A681A2408885998500000007
In [44]:
ods html close;
options nodate nonumber;
  data _null_;
    set sashelp.class (obs=5) end=eof;
    file print notitles; 
    If _n_=1 then put @5 "Children's Demographic Characteristics";
    if _n_=1 then put @5 38*'-';
    If _n_=1 then put 
         @5 'Name' +6 'Sex' +3 'Age' +1 'Height' +2 'Weight';
    if _n_=1 then put @5 38*'-';
    put  @5 name $8.  -r
         +3 sex $1.
         +3 age 3.
         +3 height 4.1
         +3 weight  6.1;
  if eof then do;
          put @5 38*'-'/;
          put @5 'Data Source: SASHELP.CLASS;' _N_ : z2. 'cases.'; 
          put @5 "Date Prepared: %sysfunc(today(), worddate).";
  end;
  run;
SAS Output

    Children's Demographic Characteristics                                                                                          

    --------------------------------------                                                                                          

    Name      Sex   Age Height  Weight                                                                                              

    --------------------------------------                                                                                          

      Alfred   M    14   69.0    112.5                                                                                              

       Alice   F    13   56.5     84.0                                                                                              

     Barbara   F    13   65.3     98.0                                                                                              

       Carol   F    14   62.8    102.5                                                                                              

       Henry   M    14   63.5    102.5                                                                                              

    --------------------------------------                                                                                          

    Data Source: SASHELP.CLASS;05 cases.                                                                                            

    Date Prepared:      June 26, 2026.                                                                                              

In [24]:
ods html close;
options nonotes nodate nonumber;
 * Create a csv file; 
  data _null_;
    set sashelp.class (obs=5) end=last;
    file 'C:\Explore\SAS\Lesson2\Lesson2Data\class_2_2.csv' dlm=',';
    If _n_=1 then put 'Name, Sex, Age, Height, Weight';
    put Name Sex Age Height Weight;
    if last then putlog "User's NOTE: Writing to the File is completed";
  run;
                                                           The SAS System

User's NOTE: Writing to the File is completed
                                                           The SAS System

E3969440A681A2408885998500000026
In [26]:
ods html close;
options nonotes nodate nonumber;
filename csv 'C:\Explore\SAS\Lesson2\Lesson2Data\class4.csv';
data _null_;
set sashelp.class end=last;
file csv dlm=',';
put ( _all_ ) (+0);
  if last then putlog "User's NOTE: Writing to the File is completed";
run;
                                                           The SAS System

User's NOTE: Writing to the File is completed
                                                           The SAS System

E3969440A681A2408885998500000028

Exporting SAS Data Set into an Excel Spreadsheet¶

In [20]:
proc export data=sashelp.class
    outfile='C:\Explore\SAS\Lesson2\Lesson2Data\sashelp_class1.csv'
    dbms=csv
    replace;
run;
                                                           The SAS System

19 records created in C:\Explore\SAS\Lesson2\Lesson2Data\sashelp_class1.csv from SASHELP.CLASS.
  
  
                                                           The SAS System

E3969440A681A2408885998500000022
In [14]:
ods html close;
options nodate nonumber nonotes;
data _null_;
  infile 'C:\Explore\SAS\Lesson2\Lesson2Data\pop2013_no_headers.txt';
  input;
  put _infile_;
run;
                                                           The SAS System

40,3,6,1,Alabama,4833722,3722241,77
40,4,9,2,Alaska,735132,547000, 74.4
40,4,8,4,Arizona,6626624,5009810,75.6
40,3,7,5,Arkansas,2959373,2249507,76
40,4,9,6,California,38332521,29157644,76.1
40,4,8,8,Colorado,5268367,4030435,76.5
40,1,1,9,Connecticut,3596080,2810514,78.2
40,3,5,10,Delaware,925749,722191,78
40,3,5,11,District of Columbia,646449,534975,82.8
40,3,5,12,Florida,19552860,15526186,79.4
40,3,5,13,Georgia,9992167,7502458,75.1
40,4,9,15,Hawaii,1404054,1096788,78.1
40,4,8,16,Idaho,1612136,1184355,73.5
40,2,3,17,Illinois,12882135,9858828,76.5
40,2,3,18,Indiana,6570902,4984875,75.9
40,2,4,19,Iowa,3090416,2366384,76.6
40,2,4,20,Kansas,2893957,2169865,75
40,3,6,21,Kentucky,4395295,3381291,76.9
40,3,7,22,Louisiana,4625470,3512513,75.9
40,1,1,23,Maine,1328302,1067026,80.3
40,3,5,24,Maryland,5928814,4584292,77.3
40,1,1,25,Massachusetts,6692824,5298878,79.2
40,2,3,26,Michigan,9895622,7650421,77.3
40,2,4,27,Minnesota,5420380,4141269,76.4
40,3,6,28,Mississippi,2991207,2253775,75.3
40,2,4,29,Missouri,6044171,4646486,76.9
40,4,8,30,Montana,1015165,791184,77.9
40,2,4,31,Nebraska,1868516,1404168,75.1
40,4,8,32,Nevada,2790136,2128531,76.3
40,1,1,33,New Hampshire,1323459,1052337,79.5
40,1,2,34,New Jersey,8899339,6877222,77.3
40,4,8,35,New Mexico,2085287,1577747,75.7
40,1,2,36,New York,19651127,15411151,78.4
40,3,5,37,North Carolina,9848060,7562455,76.8
40,2,4,38,North Dakota,723393,560705,77.5
40,2,3,39,Ohio,11570808,8920978,77.1
40,3,7,40,Oklahoma,3850568,2903541,75.4
40,4,9,41,Oregon,3930065,3072459,78.2
40,1,2,42,Pennsylvania,12773801,10058156,78.7
40,1,1,44,Rhode Island,1051511,837524,79.6
40,3,5,45,South Carolina,4774839,3695041,77.4
40,2,4,46,South Dakota,844877,636918,75.4
40,3,6,47,Tennessee,6495978,5004401,77
40,3,7,48,Texas,26448193,19406207,73.4
40,4,8,49,Utah,2900872,2004283,69.1
40,1,1,50,Vermont,626630,503929,80.4
40,3,5,51,Virginia,8260405,6395870,77.4
40,4,9,53,Washington,6971406,5375611,77.1
40,3,5,54,West Virginia,1854304,1472626,79.4
40,2,3,55,Wisconsin,5742713,4434937,77.2
40,4,8,56,Wyoming,582658,444979,76.4
                                                           The SAS System

E3969440A681A2408885998500000016