BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ronein
Onyx | Level 15

Hello

User define 2  macro varaibles: Current month (YYMM format) and number of months.

For example :

%let end=1808;

%let n=24;

The task is to create a data set that will have one column called :YYMM and will have the values (char values):

1808

1807

1806

.......

........

........

1608 (because it 24 months  back)

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ronein
Onyx | Level 15

Good solution:

 

%let end=1808;
%let n=24;

 

data dates_tbl;
do i=0 to &n. ;
YYMM=MDY(substr(compress(&end.),3,2)*1,1,substr(compress(&end.),1,2)*1);
YYMM2=intnx('month',YYMM,-i) ;
YYMM2b=put(YYMM2,yymmn4.);

YYMM3=intnx('month',YYMM2,-12,'end') ;
var_name='t'||strip(&n.-i);
call symputx (var_name,YYMM3);

var_name2='m'||strip(&n.-i);
call symputx (var_name2,YYMM2b);

format YYMM YYMM2 YYMMN4. YYMM3 date9.;
output;
end;
run;
%put &t0;
%put &t1;
%put &t2;
%put &t24;
%put &m0;
%put &m1;
%put &m2;
%put &m24;

View solution in original post

4 REPLIES 4
andreas_lds
Jade | Level 19

Seems to be related to

The solution seems to be in Create date parameters already. Use the datastep suggested by @RW9, create a dataset instead of macro-variables.

Ronein
Onyx | Level 15

Good solution:

 

%let end=1808;
%let n=24;

 

data dates_tbl;
do i=0 to &n. ;
YYMM=MDY(substr(compress(&end.),3,2)*1,1,substr(compress(&end.),1,2)*1);
YYMM2=intnx('month',YYMM,-i) ;
YYMM2b=put(YYMM2,yymmn4.);

YYMM3=intnx('month',YYMM2,-12,'end') ;
var_name='t'||strip(&n.-i);
call symputx (var_name,YYMM3);

var_name2='m'||strip(&n.-i);
call symputx (var_name2,YYMM2b);

format YYMM YYMM2 YYMMN4. YYMM3 date9.;
output;
end;
run;
%put &t0;
%put &t1;
%put &t2;
%put &t24;
%put &m0;
%put &m1;
%put &m2;
%put &m24;

Jagadishkatam
Amethyst | Level 16
%let end=1808;
%let n=24;

data have;
do i=0 to &n by 1;
do yymm=intnx('month',input(cats(&end,'01'),yymmdd6.),-i);
format yymm yymmn4.;
output;
end;
end;
run;
Thanks,
Jag
Ronein
Onyx | Level 15

I see that your code is working well but why do you have to Do loops?

Is it not better to  have one Do loop?

%let end=1808;
%let n=24;
data have;
do i=0 to &n by 1;
yymm=intnx('month',input(cats(&end,'01'),yymmdd6.),-i);
format yymm yymmn4.;
output;
end;
run;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 4 replies
  • 1278 views
  • 1 like
  • 3 in conversation