Lesson 9, Part 8: Converting Macro Variables into DATA Step Variables¶
Example 1 (Code adapted from Carpenter, 2016)¶
- The SYMGET function is used to convert the current value of a macro variable to the character value of a data step variable within a DATA step, with a default length of 200 (Carpenter, 2016).
In [3]:
*Ex20_SYMGET_RESOLVE.sas (Part 1);
ods html close;
options nonotes nosource nodate nonumber;
data Have;
Quiz1_score=70;
call symputx('weight', .05);
wgt1=symget('weight');
W_Quiz1_score=Quiz1_score*symget('weight');
W_Quiz1_score_x=Quiz1_score*input(symget('weight'), best12.);
run;
title 'Working with the SYMGET Function';
proc contents data=Have;
ods select variables;
run;
proc print data=Have;run;
Out[3]:
The CONTENTS Procedure
| Alphabetic List of Variables and Attributes | |||
|---|---|---|---|
| # | Variable | Type | Len |
| 1 | Quiz1_score | Num | 8 |
| 3 | W_Quiz1_score | Num | 8 |
| 4 | W_Quiz1_score_x | Num | 8 |
| 2 | wgt1 | Char | 200 |
| Obs | Quiz1_score | wgt1 | W_Quiz1_score | W_Quiz1_score_x |
|---|---|---|---|---|
| 1 | 70 | 0.05 | 3.5 | 3.5 |
Note for the code in the next cell¶
SYMGET Function vs. RESOLVE Function
Note that the argument of the SYMGET function is the macro variable itself and that the argument of the RESOLVE function is a macro variable reference.
Example 2¶
In [4]:
*Ex20_SYMGET_RESOLVE.sas (Part 2);
options nonotes nosource nodate nonumber;
%let factor=.05;
data Have2;
var_factor1=&factor;
var_factor2 = symget('factor');
var_factor3= resolve('&factor');
run;
title 'Working with the SYMGET and RESOLVE Functions';
proc contents data=Have2;
ods select variables;
run;
The CONTENTS Procedure
| Alphabetic List of Variables and Attributes | |||
|---|---|---|---|
| # | Variable | Type | Len |
| 1 | var_factor1 | Num | 8 |
| 2 | var_factor2 | Char | 200 |
| 3 | var_factor3 | Char | 200 |