BookmarkSubscribeRSS Feed
Kayla_Tan222
Calcite | Level 5

 

Hi all, is there a way to make the do loop to loop my code according to the way I set?

 

If i use the code like this %do i=%eval(&MM.-1) %to &MONTH; it is correct if the macro date I set at the beginning is .For Ex:

 

YYYY=2019,MM=02. The i symbol in do loop will be 1,2 and 3.

YYYY=2019,MM=03. The i symbol in do loop will be 2,3 and 4.

 

 

Now, if lets say i set the macro date to be

 

YYYY=2019,MM=01 , the i symbol in do loop supposed to be appeared as 12,1,2.  But it will help me to loop with number 0,1,2.

 

YYYY=2018,MM=12 , the i symbol in do loop supposed to be appeared as 11,12,1. But it will help me to loop with number 11,12,13.

 

 

However this is not work if using the same code above as this only follow the order of the number.

 

Is there a way to let the do loop follow the number i set to loop the code?

My idea is like this below but this is not work

 

%if &MM=01 %then %do;

%do i=12,1,2;    <---- i want the do statement loop the code with the number i set here

...........

%end;

 

%else %if &MM=12 %THEN %DO;

%do i=11,12,1;

....

%end;

 

%else %do;

%do i=%eval(&MM.-1) %to &MONTH;

%end;

 

 

 

 

 

my complete  code is below:

 

 

%macro date(YYYY=2019,MM=01,Y=2019,M=01);%global YR MTH;

 

 

libname pl&Y&M. "\\kaiwksgh415thw5\Data\POLA\Claim\&Y\&Y&M\DBF";

Data pl&Y&M..POLA_clm_3months_&Y&M.;

set pl&Y&M..POLA_clm_agg_&Y&M.;

where accyr_fy=&YYYY. & accmth_fy<=%eval(&MM.+1);

dev11yr_fy=min(accyr_fy-lossyr_fy,11);

 

run;

PROC FORMAT;

value $ class

'VEH'='1'

'FIR'='2'

'GMS'='3'

'BON'='3'

'HUL'='3'

'PAC'='4'

'TEL'='5'

'WWC'='5'

'LIA'='6'

'CGO'='7'

'ENG'='8'

 

;

RUN;

 

/************************************************************/

%macro financial (MONTH);

%do i=%eval(&MM.-1) %to &MONTH; 

 

 

%if &i.=1 %then %do;

%let YR=%eval(&YYYY-1);

%let MTH=12;

%end;

 

%else %if &i.=12 %then %do;

%let YR=&YYYY;

%let MTH=11;

%end;

 

%else %if &i.=11 %then %do;

%let YR=&YYYY;

%let MTH=10;

%end;

%else %do;

%let YR=&YYYY;

%let MTH=0%eval(&i.-1);

%end;

 

 

 

PROC SQL;

create table SMCD_PD_&YR&MTH. AS

SELECT put(zmajrsk,$class.) as class, dev11yr_fy,sum(zclmpd) AS zclmpd

FROM pl&Y&M..POLA_clm_3months_&Y&M.

WHERE accmth_fy=&i.

GROUP BY class,2;

 

run;

 

 

ods html

file="\\kaiwksgh415thw5\Data\POLA\Claim\&YR\&YR&MTH\result\SMCD_mth_PD_FY_10DY_&YR&MTH..html"

 

style=normal;

proc print;

run;

 

 

 

%end;

%mend financial;

%financial(%eval(&MM.+1));

run;

 

 

%mend date;

%date(YYYY=2019,MM=02,Y=2019,M=02);

run;

 

2 REPLIES 2
gamotte
Rhodochrosite | Level 12

Hello,

 

You can try something like this :

%macro doStuff(monthNum);
    /* The code you want to iterate on */

    %put I do some stuff for mont &monthNum.;

%mend;


data _NULL_;
    startDate='01Dec2018'd;
    endDate='01Feb2019'd;

    date=startDate;

    do while (date<=endDate);
        call execute(cats('%nrstr(%doStuff)(',put(month(date),z2.),');'));
        date=intnx('Month',date,1);
    end;
run;

ballardw
Super User

MACRO do loops can not use a list at this time the way that a data step loop will.

 

Your example looks like you are playing with dates in which case you might want to consider the INTNX function with date values instead trying to play around with raw month and year  values. That would get away from the entire "test which month I'm working with and calculate specific month previous.

 

It appears that you are attempting to get a year month string of the previous month in YYYYMM form. Please consider the following approach to build such.

%macro dummy (yyyy=, mm=);
/* get an actual date*/
%let date = %sysfunc(inputn(&yyyy.&mm.,yymmn6.));
%let date1 = %sysfunc(intnx(month,&date.,-1,B));
%let datestr= %sysfunc(putn(&date1.,yymmn6.));
%put &datestr.;

%mend;

%dummy(yyyy=2019,mm=01)

Instead of using &yr&mth you could use the DATESTR variable (or your preferred name) built as above.

 

If you need to loop over a sequence of months then you could do I= -1 to 1 and replace the -1 with &I in the INTNX function call which would yield the previous month, the current month and the next month.

Once you have the correct DATE1 value you can use all of the date related functions such as YEAR, MONTH or DAY to extract bits of the date or use other functions with the values.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 738 views
  • 0 likes
  • 3 in conversation