BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
somebody
Lapis Lazuli | Level 10

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Please post test data in the form of a datastep:

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

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.

View solution in original post

7 REPLIES 7
Jagadishkatam
Amethyst | Level 16
%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;
Thanks,
Jag
somebody
Lapis Lazuli | Level 10

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. 

andreas_lds
Jade | Level 19

Do you have a date or only the month in the dataset?

 

You can extract the month by using

%sysfunc(month(&start.))

 

somebody
Lapis Lazuli | Level 10

I have monthly observations

Tom
Super User Tom
Super User

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;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Please post test data in the form of a datastep:

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

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.

somebody
Lapis Lazuli | Level 10

thanks. it is indeed the best solution 

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
  • 7 replies
  • 3040 views
  • 1 like
  • 5 in conversation