Lesson 9, Part 2: The CALL SYMPUTX Routine¶
In [1]:
*Ex1_Motivation_for_macro_variables (Part 1);
options nocenter nodate notes nonumber nosource;
ods html close;
proc means data=sashelp.class mean maxdec=1 noprint;
var weight;
output out=work.stats (drop = _TYPE_ _FREQ_) mean=average_wgt;
run;
5 The SAS System 13:19 Friday, March 29, 2024 24 ods listing close;ods html5 (id=saspy_internal) file=_tomods1 options(bitmap_mode='inline') device=svg style=HTMLBlue; 24 ! ods graphics on / outputfmt=png; NOTE: Writing HTML5(SASPY_INTERNAL) Body file: _TOMODS1 25 26 *Ex1_Motivation_for_macro_variables (Part 1); 27 options nocenter nodate notes nonumber nosource; NOTE: There were 19 observations read from the data set SASHELP.CLASS. NOTE: The data set WORK.STATS has 1 observations and 1 variables. NOTE: PROCEDURE MEANS used (Total process time): real time 0.03 seconds cpu time 0.01 seconds The SAS System E3969440A681A2408885998500000003
*Ex1_Motivation_for_macro_variables (Part 2);
Data test;
set SASHELP.class;
weight_ratio=weight/average_wgt; /*This line of code does not work*/
run;
In [2]:
*Ex1_Motivation_for_macro_variables (Part 3);
* Create a macro variable using CALL SYMPUTX;
options nocenter nodate nonumber nosource;
ods html close;
data _null_;
set work.stats;
call symputx('average_wgt', average_wgt);
run;
%put _user_;
The SAS System NOTE: Writing HTML5(SASPY_INTERNAL) Body file: _TOMODS1 NOTE: There were 1 observations read from the data set WORK.STATS. NOTE: DATA statement used (Total process time): real time 0.01 seconds cpu time 0.00 seconds GLOBAL AVERAGE_WGT 100.02631579 The SAS System E3969440A681A2408885998500000004
In [3]:
Data test;
set SASHELP.class;
weight_ratio=weight/&average_wgt;
run;
proc print data=test;
run;
| Obs | Name | Sex | Age | Height | Weight | weight_ratio |
|---|---|---|---|---|---|---|
| 1 | Alfred | M | 14 | 69.0 | 112.5 | 1.12470 |
| 2 | Alice | F | 13 | 56.5 | 84.0 | 0.83978 |
| 3 | Barbara | F | 13 | 65.3 | 98.0 | 0.97974 |
| 4 | Carol | F | 14 | 62.8 | 102.5 | 1.02473 |
| 5 | Henry | M | 14 | 63.5 | 102.5 | 1.02473 |
| 6 | James | M | 12 | 57.3 | 83.0 | 0.82978 |
| 7 | Jane | F | 12 | 59.8 | 84.5 | 0.84478 |
| 8 | Janet | F | 15 | 62.5 | 112.5 | 1.12470 |
| 9 | Jeffrey | M | 13 | 62.5 | 84.0 | 0.83978 |
| 10 | John | M | 12 | 59.0 | 99.5 | 0.99474 |
| 11 | Joyce | F | 11 | 51.3 | 50.5 | 0.50487 |
| 12 | Judy | F | 14 | 64.3 | 90.0 | 0.89976 |
| 13 | Louise | F | 12 | 56.3 | 77.0 | 0.76980 |
| 14 | Mary | F | 15 | 66.5 | 112.0 | 1.11971 |
| 15 | Philip | M | 16 | 72.0 | 150.0 | 1.49961 |
| 16 | Robert | M | 12 | 64.8 | 128.0 | 1.27966 |
| 17 | Ronald | M | 15 | 67.0 | 133.0 | 1.32965 |
| 18 | Thomas | M | 11 | 57.5 | 85.0 | 0.84978 |
| 19 | William | M | 15 | 66.5 | 112.0 | 1.11971 |
In [3]:
options nocenter nodate nonumber nosource;
ods html close;
data _null_;
set sashelp.class;
call symputx(cats('Name', _N_),Name );
run;
%put _user_;
Out[3]:
The SAS System
NOTE: Writing HTML5(SASPY_INTERNAL) Body file: _TOMODS1
NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
GLOBAL AVERAGE_WGT 100.02631579
GLOBAL NAME1 Alfred
GLOBAL NAME10 John
GLOBAL NAME11 Joyce
GLOBAL NAME12 Judy
GLOBAL NAME13 Louise
GLOBAL NAME14 Mary
GLOBAL NAME15 Philip
GLOBAL NAME16 Robert
GLOBAL NAME17 Ronald
GLOBAL NAME18 Thomas
GLOBAL NAME19 William
GLOBAL NAME2 Alice
GLOBAL NAME3 Barbara
GLOBAL NAME4 Carol
GLOBAL NAME5 Henry
GLOBAL NAME6 James
GLOBAL NAME7 Jane
GLOBAL NAME8 Janet
GLOBAL NAME9 Jeffrey
E3969440A681A2408885998500000006
CALL SYMPUTX in the above code snippet¶
This routine has two parameters separated by a comma inside its outer parentheses.
The first parameter, a constant enclosed in quote marks conctaenated with the value of the automatic variable N, will be the name of the macro variable being created.
The second parameter will be the value of the DATA step vatribale NAME being assigned to each macro variable.
The code creates 19 macro variables, and the value of of each of those macro variables is a character string. These macro variable reside 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 [ ]:
%showLog
In [8]:
*Ex10_create_macro_vars_call_symputx_1.sas (Part 2);
*Code idea adapted from Carpenter (2016);
data S1;
input mvar $ mvalue $;
datalines;
Course Stat6197
Exam Midterm
Mean 73
SD 7
;
run;
data _null_;
set S1;
call symputx(mvar, mvalue);
run;
%put _USER_;
Out[8]:
The SAS System
NOTE: Writing HTML5(SASPY_INTERNAL) Body file: _TOMODS1
NOTE: The data set WORK.S1 has 4 observations and 2 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
NOTE: There were 4 observations read from the data set WORK.S1.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.01 seconds
GLOBAL AVERAGE_WGT 100.02631579
GLOBAL COURSE Stat6197
GLOBAL EXAM Midterm
GLOBAL MEAN 73
GLOBAL SD 7
E3969440A681A2408885998500000011
In [9]:
*Ex10_create_macro_vars_call_symputx_1.sas (Part 3);
%macro mvarlist;
data _null_;
set S1;
call symputx(mvar, mvalue, 'L');
run;
%put _Local_;
%mend mvarlist;
%mvarlist
Out[9]:
The SAS System
NOTE: Writing HTML5(SASPY_INTERNAL) Body file: _TOMODS1
NOTE: There were 4 observations read from the data set WORK.S1.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
MVARLIST COURSE Stat6197
MVARLIST EXAM Midterm
MVARLIST MEAN 73
MVARLIST SD 7
E3969440A681A2408885998500000012
In the code below, during the last iteration of the DATA step when LAST=1, the CALL SYMPUTX routine creates a macro variable named NUMBER whose value is the value of the DATA step variable N. The DATA step variable N is assigned to the macro variable NUMBER.¶
The value of the macro variable &Number is inserted into the FOOTNOTE statement.¶
In [5]:
title;
*Ex10_create_macro_vars_call_symputx_1.sas (Part 5);
data Have;
options nocenter;
set SASHELP.CLASS end=last;
if age >14 then do;
n+1;
if last then call symputx('number', n);
output;
end;
run;
Footnote "There are &number of observations with AGE>14";
proc print data=have; run;
Footnote;
Out[5]:
| Obs | Name | Sex | Age | Height | Weight | n |
|---|---|---|---|---|---|---|
| 1 | Janet | F | 15 | 62.5 | 112.5 | 1 |
| 2 | Mary | F | 15 | 66.5 | 112.0 | 2 |
| 3 | Philip | M | 16 | 72.0 | 150.0 | 3 |
| 4 | Ronald | M | 15 | 67.0 | 133.0 | 4 |
| 5 | William | M | 15 | 66.5 | 112.0 | 5 |
In [7]:
*Ex10_create_macro_vars_call_symputx_1.sas (Part 6);
options nocenter nonumber nodate nonotes nosource;
ods html close;
DATA _NULL_;
set sashelp.class;
call symputx('Name' || STRIP(put( _N_, 2.)), Name);
run;
%put &Name1 &Name10 &Name13;
Out[7]:
The SAS System
Alfred John Louise
E3969440A681A2408885998500000010