Hi all;
I am writing this conditional macro variable and I think I'm missing something. Also, I wasn't able to see how it resolve by %put statement and compile note option
this is the code
OPTIONS SYMBOLGEN mprint;
*OPTIONS MPRINT;
%let Year=2018;
%let Qtr=1;
%macro q1;
%let lastqyear=%eval(&year-1);
%let lastq=4;
%mend;
%macro otherq;
%let lastqyear=&year;
%let lastq=%eval(&qtr-1);
%mend;
%global lastQyear lastq;
%macro thisq;
%if &qtr=1 %then %do; %Q1; %end; %else %do; %otherq; %end;
%mend;
%let outpath=Z:\Reporting\Safety Reporting\2018\QC\AE_2018Mar30;
%let Hardlock=Z:\Reporting\Safety Reporting\&Year\Extract Data\SDTM\q&qtr;
%let Phlext=Z:\Reporting\Safety Reporting\&lastqyear\Extract Data\SDTM\q&lastq;
Why are you making things so complicated?
%let Year=2018;
%let Qtr=1;
data _null_;
previous=intnx('qtr',yyq(&year,&qtr),-1);
call symputx('lastqyear',year(previous),'g');
call symputx('lastq',qtr(previous),'g');
run;
1577 %put &=year &=qtr; YEAR=2018 QTR=1 1578 %put &=lastqyear &=lastq; LASTQYEAR=2017 LASTQ=4
Your macro variables lastyear and lastq do not exist after the macros Q1 and OtherQ finish executing because the variables would be treated as local.
You would need to add
%global lastyear lastq;
to each of those macros before assigning the values to the variables.
Look up Macro Variable Scope
it is still not working even after I put the semicolon
You're missing two things.
%LET statements need to end with a semicolon. You have a bunch of them missing. Perhaps they are missing only in what you posted, and not in what you actually ran.
The second item is that the newly created macro variables (LASTQYEAR and LASTQ) are %local to the macro that defines them. You need to add this statement:
%global lastqyear lastq;
The easiest place to add it is at the beginning of the %RESOLVE macro. That way, you only need to add it once.
this is the error msg after I tried global statement
131
132 OPTIONS SYMBOLGEN mprint;
133 *OPTIONS MPRINT;
134
135 %let Year=2018;
136 %let Qtr=1;
137
138
139 %macro q1;
140 %let lastqyear=%eval(&year-1);
141 %let lastq=4;
142 %mend;
143
144 %macro otherq;
145 %let lastqyear=&year;
146 %let lastq=%eval(&qtr-1);
147
148 %mend;
149
150 %global lastyear lastq;
151
152 %macro resolve;
ERROR: Macro RESOLVE has been given a reserved name.
ERROR: A dummy macro will be compiled.
153 %if &qtr=1 %then %do; %Q1; %end; %else %do; %otherq; %end;
154
155 %mend;
156
157
158
159
160 %let outpath=Z:\Reporting\Safety Reporting\2018\QC\AE_2018Mar30;
161
162 %let Hardlock=Z:\Reporting\Safety Reporting\&Year\Extract Data\SDTM\q&qtr;
SYMBOLGEN: Macro variable YEAR resolves to 2018
SYMBOLGEN: Macro variable QTR resolves to 1
163 %let Phlext=Z:\Reporting\Safety Reporting\&lastqyear\Extract Data\SDTM\q&lastq;
WARNING: Apparent symbolic reference LASTQYEAR not resolved.
SYMBOLGEN: Macro variable LASTQ resolves to
This error means you have to change the name of the macro. RESOLVE is not a legal name.
now this is the error msg
197
198 OPTIONS SYMBOLGEN mprint;
199 *OPTIONS MPRINT;
200
201 %let Year=2018;
202 %let Qtr=1;
203
204
205 %macro q1;
206 %let lastqyear=%eval(&year-1);
207 %let lastq=4;
208 %mend;
209
210 %macro otherq;
211 %let lastqyear=&year;
212 %let lastq=%eval(&qtr-1);
213
214 %mend;
215
216 %global lastyear lastq;
217
218 %macro thisq;
219 %if &qtr=1 %then %do; %Q1; %end; %else %do; %otherq; %end;
220
221 %mend;
222
223
224
225
226 %let outpath=Z:\Reporting\Safety Reporting\2018\QC\AE_2018Mar30;
227
228 %let Hardlock=Z:\Reporting\Safety Reporting\&Year\Extract Data\SDTM\q&qtr;
SYMBOLGEN: Macro variable YEAR resolves to 2018
SYMBOLGEN: Macro variable QTR resolves to 1
229 %let Phlext=Z:\Reporting\Safety Reporting\&lastqyear\Extract Data\SDTM\q&lastq;
WARNING: Apparent symbolic reference LASTQYEAR not resolved.
SYMBOLGEN: Macro variable LASTQ resolves to
Your %GLOBAL statement defined lastyear but your code referenced lastQyear .
You defined three macros, but you did not call any of them.
Just edited the code and the macro didn't resolve
263
264 OPTIONS SYMBOLGEN mprint;
265 *OPTIONS MPRINT;
266
267 %let Year=2018;
268 %let Qtr=1;
269
270
271 %macro q1;
272 %let lastqyear=%eval(&year-1);
273 %let lastq=4;
274 %mend;
275
276 %macro otherq;
277 %let lastqyear=&year;
278 %let lastq=%eval(&qtr-1);
279
280 %mend;
281
282 %global lastQyear lastq;
283
284 %macro thisq;
285 %if &qtr=1 %then %do; %Q1; %end; %else %do; %otherq; %end;
286
287 %mend;
288
289
290
291
292 %let outpath=Z:\Reporting\Safety Reporting\2018\QC\AE_2018Mar30;
293
294 %let Hardlock=Z:\Reporting\Safety Reporting\&Year\Extract Data\SDTM\q&qtr;
SYMBOLGEN: Macro variable YEAR resolves to 2018
SYMBOLGEN: Macro variable QTR resolves to 1
295 %let Phlext=Z:\Reporting\Safety Reporting\&lastqyear\Extract Data\SDTM\q&lastq;
SYMBOLGEN: Macro variable LASTQYEAR resolves to
SYMBOLGEN: Macro variable LASTQ resolves to
Looks like it did what you asked.
You probably want to run one of those macros you defined.
Try adding this line before the last to %LET statements:
%thisq
Why are you making things so complicated?
%let Year=2018;
%let Qtr=1;
data _null_;
previous=intnx('qtr',yyq(&year,&qtr),-1);
call symputx('lastqyear',year(previous),'g');
call symputx('lastq',qtr(previous),'g');
run;
1577 %put &=year &=qtr; YEAR=2018 QTR=1 1578 %put &=lastqyear &=lastq; LASTQYEAR=2017 LASTQ=4
this code is good put I still have limitations since I need the code to look to the same year if it the q2,q3,q4
@mona4u wrote:
this code is good put I still have limitations since I need the code to look to the same year if it the q2,q3,q4
Have you even tried it before posting this?
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.