I want to create/assign macro variables by adding and substracting variables in the dataset.
Any suggestions would be appreciated. Thank you. -- George
The code and dataset are attached.
The code is:
libname sasdb "C:\grezek\CRE CECL" ;data _null_ ;
call symputx('d1','2016Q4');
call symputx('d2','2017Q1');
call symputx('d3','2017Q2');
call symputx('d4','2017Q3');
call symputx('d5','2017Q4');
call symputx('d6','2018Q1');
call symputx('d7','2018Q2');
call symputx('d8','2018Q3');
call symputx('d9','2018Q4');
call symputx('d10','2019Q1');
options symbolgen macrogen mprint ;
%macro amort ;
%do i = 1 %to 10 ;
%let amort2_&&d&i = %sysevalf(amort_&&d&i.. + Net_financial_amount_&&d&i.. - Net_financial_amount_next_&&d&i..) ;
%end ;
run ;
%mend ;
run ;
data sasdb.header_amort3 ;
set sasdb.header_amort2 ;
%amort ;
run ;
The log is (although I've tried quite of number of alternative, none seem to make the macro variable resolve to numbers):
83 libname sasdb "C:\grezek\CRE CECL" ;
NOTE: Libref SASDB was successfully assigned as follows:
Engine: V9
Physical Name: C:\grezek\CRE CECL
83 ! data _null_ ;
84 call symputx('d1','2016Q4');
85 call symputx('d2','2017Q1');
86 call symputx('d3','2017Q2');
87 call symputx('d4','2017Q3');
88 call symputx('d5','2017Q4');
89 call symputx('d6','2018Q1');
90 call symputx('d7','2018Q2');
91 call symputx('d8','2018Q3');
92 call symputx('d9','2018Q4');
93 call symputx('d10','2019Q1');
94 options symbolgen macrogen mprint ;
95
96 %macro amort ;
97
98 %do i = 1 %to 10 ;
99 %let amort2_&&d&i = %sysevalf(amort_&&d&i.. + Net_financial_amount_&&d&i.. -
99 ! Net_financial_amount_next_&&d&i..) ;
100 %end ;
101 run ;
102 %mend ;
103 run ;
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
104
105 data sasdb.header_amort3 ;
106 set sasdb.header_amort2 ;
107
108 %amort ;
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable I resolves to 1
SYMBOLGEN: Macro variable D1 resolves to 2016Q4
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable I resolves to 1
SYMBOLGEN: Macro variable D1 resolves to 2016Q4
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable I resolves to 1
SYMBOLGEN: Macro variable D1 resolves to 2016Q4
SYMBOLGEN: && resolves to &.
SYMBOLGEN: Macro variable I resolves to 1
SYMBOLGEN: Macro variable D1 resolves to 2016Q4
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric
operand is required. The condition was: amort_2016Q4 + Net_financial_amount_2016Q4 -
Net_financial_amount_next_2016Q4
ERROR: The macro AMORT will stop executing.
109 run ;
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set SASDB.HEADER_AMORT3 may be incomplete. When this step was stopped there
were 0 observations and 28 variables.
WARNING: Data set SASDB.HEADER_AMORT3 was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
You could do this whole thing with ARRAYs and not ever have to use a macro or macro variable, and not ever have the complications that you are running into with macro variables. Simplify, that would be a good thing.
However, %SYSEVALF expects macro variables, not data step variables. I assume that
amort_2016Q4 + Net_financial_amount_2016Q4 - Net_financial_amount_next_2016Q4
refer to data step variables. Since you appear to want to add or subtract these data step variables, you could do this with data step commands (which adds or subtracts data step variables) and not with the macro function %SYSEVALF (which adds or subtracts macro variables):
%do i = 1 %to 10 ;
amort2_&&d&i = amort_&&d&i.. + Net_financial_amount_&&d&i.. - Net_financial_amount_next_&&d&i.. ;
%end ;
Remember, macro variables, when resolved must produce legal valid SAS code, and if you first create legal valid SAS code without macros and without macro variables for one or two cases, once you have that working, you could more easily make a macro work. If you don't have the code working without macro variables and without macros, you will never get it to work with macros. Or, as I strongly advise, use ARRAYs and not macros.
You could do this whole thing with ARRAYs and not ever have to use a macro or macro variable, and not ever have the complications that you are running into with macro variables. Simplify, that would be a good thing.
However, %SYSEVALF expects macro variables, not data step variables. I assume that
amort_2016Q4 + Net_financial_amount_2016Q4 - Net_financial_amount_next_2016Q4
refer to data step variables. Since you appear to want to add or subtract these data step variables, you could do this with data step commands (which adds or subtracts data step variables) and not with the macro function %SYSEVALF (which adds or subtracts macro variables):
%do i = 1 %to 10 ;
amort2_&&d&i = amort_&&d&i.. + Net_financial_amount_&&d&i.. - Net_financial_amount_next_&&d&i.. ;
%end ;
Remember, macro variables, when resolved must produce legal valid SAS code, and if you first create legal valid SAS code without macros and without macro variables for one or two cases, once you have that working, you could more easily make a macro work. If you don't have the code working without macro variables and without macros, you will never get it to work with macros. Or, as I strongly advise, use ARRAYs and not macros.
You're absolutely right, there are other ways of attacking this which I should have tried.
Thank you for the solution and the explanation. This helps me out quite a bit! Thanks, again. -- George
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.