I am trying to write a macro that draw graph every month, thus it requires a DO loop with increment by month. How do I do that ? My code are:
%macro TS_animation(dataset=,start=, end=, incr=);
%do month=&start %to &end %by &incr;
proc sgplot data= &dataset.;
.......
run;
%end;
%mend TS_animation;
Please post test data in the form of a datastep:
Its really not worth our time to be guessing what you have!
As for how you do these things, the simplest cleanest method is to put month into your data a a variable - datasets are where data should go - and then in your proc sgplot you use a by group with the month variable. e.g.:
proc sgplot data=have; by month; run;
This is far simpler coding, will run a lot quicker using less resources.
%let start=1;
%let end=12;
%let incr=1;
/*create a month variable in the dataset you are passing and use the where to subset by do loop*/
%macro TS_animation(dataset=,start=, end=, incr=);
%do month=&start %to &end %by &incr;
proc sgplot data= &dataset.;
where month=&month;
.......
run;
%end;
%mend TS_animation;
Thank you for your answer but I would like to input time (month) values for the start and end, for example, when I call the macro, it would be like this :
%TS_anomation( dataset=sample, start = '01Jan2000'd, end='31Dec2010'd);
The way I understand you answer is that the month variable is in an integer.
Do you have a date or only the month in the dataset?
You can extract the month by using
%sysfunc(month(&start.))
I have monthly observations
Do you want to increment an INTEGER values like in your code?
Or do you want to create a DATE value?
Either way what do your want to DO with the value? Your example does not show the MONTH value being used in any way.
I would assume that you want to just generate a date value. Perhaps something like this?
%local offset date1 date2;
%do offset = 0 %to %sysfunc(intck(month,&start,&end)) %by &incr ;
%let date1 = %sysfunc(intnx(month,&start,&offset));
%let date2 = %sysfunc(intnx(month,&start,&offset+&incr));
...
where datevar between &date1 and &date2 ;
...
%end;
Please post test data in the form of a datastep:
Its really not worth our time to be guessing what you have!
As for how you do these things, the simplest cleanest method is to put month into your data a a variable - datasets are where data should go - and then in your proc sgplot you use a by group with the month variable. e.g.:
proc sgplot data=have; by month; run;
This is far simpler coding, will run a lot quicker using less resources.
thanks. it is indeed the best solution
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.