Correct syntax for DO LOOP

Accepted Solution Solved
Reply
Contributor
Posts: 51
Accepted Solution

Correct syntax for DO LOOP

While the statement:

%FAQ_MACRO2(YR = 2009, MTH = 01, PROD = "");

works perfectly in calling a macro and inserting the values of arguments wherever required, i.e. 2009, 01 and "", in the Macro, the following code fails to do so.
Any idea why the following loop fails:

data _NULL_;

x=2007;

y=1;

do x = 2007 to 2012;

do y = 1 to 12;

  %FAQ_MACRO2(YR = x, MTH = y, PROD = "");

  end;

end;

quit;

The reason is to have the macro applied for every month data for the year 2007 to 2012;

a simple example in this regard shall be highly obliged.


Accepted Solutions
Solution
‎09-16-2013 07:42 AM
Super Contributor
Posts: 282

Re: Correct syntax for DO LOOP

Hi,

In answer to your question "Any idea why the following loop fails", the macro code is being executed before the data step code, so the arguments being passed to the macro function are literally the letter x, the letter y and a pair double quotes; no data step variable *values* are in existence at this point. After the macro function has been resolved then the data step code is executed, but the x and y arguments passed to the macro function have already been passed as letters, not values.

Alternatively, if the macro function has a data step in it, then the data statement in the macro would terminate the "data _null_" statement in your SAS code.

As you are not reading a SAS data set or other external data, you might want to consider only using macro code, e.g.:

%macro yearloop(from_y,to_y);

  %do x=&from_y %to &to_y;

    %do y=1 %to 12;

      %FAQ_MACRO2(YR = &x, MTH = &y, PROD = "");

    %end;

  %end;

%mend yearloop;

%yearloop(2007,2012);

If this does not help then perhaps share the code used in %FAQ_MACRO2.

Regards,

Amir.

Message was edited by: Amir Malik - formatting.

View solution in original post


All Replies
Super User
Posts: 5,256

Re: Correct syntax for DO LOOP

With this kind of interaction between data step and macro calls, you need to use the CALL EXECUTE construct instead.

Data never sleeps
Solution
‎09-16-2013 07:42 AM
Super Contributor
Posts: 282

Re: Correct syntax for DO LOOP

Hi,

In answer to your question "Any idea why the following loop fails", the macro code is being executed before the data step code, so the arguments being passed to the macro function are literally the letter x, the letter y and a pair double quotes; no data step variable *values* are in existence at this point. After the macro function has been resolved then the data step code is executed, but the x and y arguments passed to the macro function have already been passed as letters, not values.

Alternatively, if the macro function has a data step in it, then the data statement in the macro would terminate the "data _null_" statement in your SAS code.

As you are not reading a SAS data set or other external data, you might want to consider only using macro code, e.g.:

%macro yearloop(from_y,to_y);

  %do x=&from_y %to &to_y;

    %do y=1 %to 12;

      %FAQ_MACRO2(YR = &x, MTH = &y, PROD = "");

    %end;

  %end;

%mend yearloop;

%yearloop(2007,2012);

If this does not help then perhaps share the code used in %FAQ_MACRO2.

Regards,

Amir.

Message was edited by: Amir Malik - formatting.

Contributor
Posts: 51

Re: Correct syntax for DO LOOP

Excellent explanation of the said approach Amir! Got your idea! So in other words the Do Loop needs to be carried out by an additional macro itself?

Your recommended approach sure did clarify the condition and the best approach!

Thanx again brother.

By the way any idea how to format and provide the months' numbers in Two Digits as the files the %FAQMACRO refers to have names like 2010_01, 2011_03 and therefore only the two digits arguments (i.e. the month 10-12) are being successful.

Contributor
Posts: 51

Re: Correct syntax for DO LOOP

I guess the modified version as follows would cater the needs appropriately:

%macro yearloop(from_y,to_y);

  %do x=&from_y %to &to_y;

    %do y=1 %to 12;

   %IF &y < 10 %THEN %DO;

   %FAQ_MACRO2(YR = &x, MTH = 0&y, PROD = "");

   %END;

   %ELSE %DO;

      %FAQ_MACRO2(YR = &x, MTH = &y, PROD = "");

   %END;

    %end;

  %end;

%mend yearloop;

%yearloop(2007,2012);

Super Contributor
Posts: 282

Re: Correct syntax for DO LOOP

Hi,

Yes, that should also work, and is very clear in what it is doing, albeit a little more coding.

Regards,

Amir.

Super Contributor
Posts: 282

Re: Correct syntax for DO LOOP

Hi,

Thank you. One approach could be  to use:

%FAQ_MACRO2(YR = &x, MTH = %substr(0&y,%length(&y)), PROD = "");

Other approaches will be available too, for example by using %sysfunc:

%FAQ_MACRO2(YR = &x, MTH = %sysfunc(putn(&y,z2.)), PROD = "");

which might be clearer to read / debug.

Regards,

Amir.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 245 views
  • 3 likes
  • 3 in conversation