Lesson 11, Part 4: Generating Parts of an SAS Statement Using a Macro¶

The macro below conditionally adds a variable (based on the parameter value to the macro call) in the PROC FREQ table statement.

  • %sysfunc(ifc(condition, value_if_true, value_if_false)) is a way to do inline logic.
  • %length(&row_var) checks if row_var is provided.
  • If yes → row_var * smoking_status
  • If no → just smoking_status
%let tbl = %sysfunc(ifc(%length(&row_var), &row_var * , ))smoking_status;
If row_var is empty → tbl = smoking_status
If row_var is specified → tbl = row_var * smoking_status
In [53]:
* Ex15_macro_part_of_SAS_statement.sas;
ods html close;  /* close default HTML to avoid clutter, optional */
options nocenter nodate nonumber symbolgen;
%macro run_freq(row_var);
   /* Build table expression dynamically */
   %let tbl = %sysfunc(ifc(%length(&row_var), &row_var * , ))smoking_status;

   
   /* Create a small dataset to display macro variable */
   data tbl_debug;
       length info $50;
       info = "&tbl";
   run;

   /* Print macro variable inline in Jupyter notebook */
    proc print data=tbl_debug noobs label;
       label info="DEBUG: TBL";
   run;


   /* Run PROC FREQ */
   proc freq data=sashelp.heart;
      tables &tbl;
   run;
%mend run_freq;
The SAS System

E3969440A681A2408885998500000051
In [55]:
%run_freq()
%run_freq(sex)
%run_freq(Weight_Status)
SAS Output

The SAS System

DEBUG: TBL
smoking_status

The SAS System

The FREQ Procedure

Smoking Status
Smoking_Status Frequency Percent Cumulative
Frequency
Cumulative
Percent
Frequency Missing = 36
Heavy (16-25) 1046 20.22 1046 20.22
Light (1-5) 579 11.19 1625 31.41
Moderate (6-15) 576 11.13 2201 42.55
Non-smoker 2501 48.35 4702 90.90
Very Heavy (> 25) 471 9.10 5173 100.00

The SAS System

DEBUG: TBL
sex *smoking_status

The SAS System

The FREQ Procedure

Frequency
Percent
Row Pct
Col Pct
Table of Sex by Smoking_Status
Sex Smoking_Status(Smoking Status)
Heavy (16-25) Light (1-5) Moderate (6-15) Non-smoker Very Heavy (> 25) Total
Female
339
6.55
11.87
32.41
422
8.16
14.78
72.88
340
6.57
11.90
59.03
1682
32.51
58.89
67.25
73
1.41
2.56
15.50
2856
55.21
 
 
Male
707
13.67
30.51
67.59
157
3.03
6.78
27.12
236
4.56
10.19
40.97
819
15.83
35.35
32.75
398
7.69
17.18
84.50
2317
44.79
 
 
Total
1046
20.22
579
11.19
576
11.13
2501
48.35
471
9.10
5173
100.00
Frequency Missing = 36

The SAS System

DEBUG: TBL
Weight_Status *smoking_status

The SAS System

The FREQ Procedure

Frequency
Percent
Row Pct
Col Pct
Table of Weight_Status by Smoking_Status
Weight_Status(Weight Status) Smoking_Status(Smoking Status)
Heavy (16-25) Light (1-5) Moderate (6-15) Non-smoker Very Heavy (> 25) Total
Normal
381
7.37
26.06
36.42
171
3.31
11.70
29.58
234
4.53
16.01
40.70
543
10.51
37.14
21.73
133
2.57
9.10
28.36
1462
28.29
 
 
Overweight
618
11.96
17.52
59.08
375
7.26
10.63
64.88
306
5.92
8.68
53.22
1903
36.83
53.96
76.15
325
6.29
9.21
69.30
3527
68.26
 
 
Underweight
47
0.91
26.40
4.49
32
0.62
17.98
5.54
35
0.68
19.66
6.09
53
1.03
29.78
2.12
11
0.21
6.18
2.35
178
3.44
 
 
Total
1046
20.24
578
11.19
575
11.13
2499
48.36
469
9.08
5167
100.00
Frequency Missing = 42
In [ ]:
%showLog
In [ ]: