geetha
geetha

Reputation: 23

Create macro dates in loop

I wrote below part to get the third month from date macro.

%let date=2017-01-01; 
%let a_SASdate=%sysfunc(inputn(&date.,yymmdd10.)) ;
%let b=%sysfunc(putn(&a_SASdate.,yymmn6.)) ;
%let et=%sysfunc(intnx(month,%sysfunc(inputn(&date.,yymmdd10.)),2,s),yymmn6.);
%put  &a_SASdate. &b. &et.;

I wrote below code to create macro variable for each date.

data new;
do i=1 to 12;
call symput('mon'||put(i,z2.),put(intnx('month',&et.,i),yymmn6.));
a=symget('mon'||put(i,z2.));
output;
end;
run;

Expected output

i   a
1   201704
2   201705
3   201706
4   201707
5   201708
6   201709
7   201710
8   201711
9   201712
10  201801
11  201801
12  201803

But what iam getting is

1   251204
2   251205
3   251206
4   251207
5   251208
6   251209
7   251210
8   251211
9   251212
10  251301
11  251302
12  251303

What went wrong?

Upvotes: 0

Views: 284

Answers (2)

Shenglin Chen
Shenglin Chen

Reputation: 4554

When &et was resolved to 201703, which is not SAS date, so based on wrong date, intnx('month',&et.,i) gave you wrong results. So you have to convert &et to SAS date first. In addition, you just want define a variable, you don't need multiple macro variable.

data new;
do i=1 to 12;
call symput('a',put(intnx('month',input("&et",yymmn6.),i),yymmn6.));
a=symget('a');
output;
end;
run;

Upvotes: 2

pinegulf
pinegulf

Reputation: 1396

So you want to have year and month for next 12 months from beginning of certain date. I came up with a bit more compact solution:

%let date= '1jan17'd; /*Begin date*/

data wanted; 
    do i=1 to 12;
        a=intnx('month',&date.,i); /*increment by single month from begin date*/
        a=put(a, yymmn6.); /*This formats the date to wanted. */
        output;
    end;
run;

For more on relevant functions on IntNx and YYMMxw. Format

Upvotes: 1

Related Questions