Hello,
The sample macro do loop coding didn't work. Please help.
%let X1=21;
%let X2=22;
%let X3=23;
%Macro aaa (Pathogen);
data Want;
set Have;
%do i = 1 %to 3;
If &&X&i..01 <=WeekCode<= &&X&i..13 then Quarter=1;
else if If &&X&i..14 <=WeekCode<= &&X&i..26 then Quarter=2;
else if If &&X&i..27 <=WeekCode<= &&X&i..39 then Quarter=3;
else if If &&X&i..40 <=WeekCode<= &&X&i..52 then Quarter=4;
%end;;
run;
%mend;
%aaa (RSV);
The error message was partially shown in the log window below.
SYMBOLGEN: && resolves to &. SYMBOLGEN: Macro variable I resolves to 3 SYMBOLGEN: Macro variable X3 resolves to 23 MPRINT(AAA): else if If 2327 <=WeekCode <= 2339 then Quarter=3; SYMBOLGEN: && resolves to &. SYMBOLGEN: Macro variable I resolves to 3 SYMBOLGEN: Macro variable X3 resolves to 23 NOTE: Line generated by the macro variable "X3". 1 2340 ---- 22 SYMBOLGEN: && resolves to &. SYMBOLGEN: Macro variable I resolves to 3 SYMBOLGEN: Macro variable X3 resolves to 23 MPRINT(AAA): else if If 2340 <=WeekCode <= 2352 then Quarter=4; MLOGIC(AAA): %DO loop index variable I is now 4; loop will not iterate again. MPRINT(AAA): ; MPRINT(AAA): run; ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, (, *, **, +, -, /, <, <=, <>, =, >, ><, >=, AND, EQ, GE, GT, LE, LT, MAX, MIN, NE, NG, NL, OR, [, ^=, {, |, ||, ~=. NOTE: The SAS System stopped processing this step because of errors. WARNING: The data set WORK.RSV may be incomplete. When this step was stopped there were 0 observations and 6 variables. NOTE: DATA statement used (Total process time): real time 0.99 seconds cpu time 0.06 seconds
if If
Two IFs in succession can't work.
if If
Two IFs in succession can't work.
else if If &&X&i..14 <=WeekCode<= &&X&i..26 then Quarter=2; else if If &&X&i..27 <=WeekCode<= &&X&i..39 then Quarter=3; else if If &&X&i..40 <=WeekCode<= &&X&i..52 then Quarter=4;
else if if.. Could you try to remove one of IF's?
If instead of something odd like "weekcode" you had an actual DATE value then the QTR function would return the value from the date and this macro wouldn't be needed.
Or whatever need is served by this 'quarter' (and week) value could likely be accomplished with a proper format in analysis or reporting.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.