BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ybz12003
Rhodochrosite | Level 12

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
1 ACCEPTED SOLUTION
3 REPLIES 3
A_Kh
Lapis Lazuli | Level 10
		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?

ballardw
Super User

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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 382 views
  • 3 likes
  • 4 in conversation