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

I have a program that I'm trying to build that will read in files for each month.  It will be a user needing to enter how many months were in and then letting it run.  For example the following.  If they ran it today they would only need to run the first 3, but I want it to be that they could just enter the number 3 and my program will run the first three repeat macros.  Does that make sense.  I then then those three datasets to set together.  So I would like to be able to enter 3 and it will run through this macro 3 times (March) and then set the 3 datasets together.  Next month I would come in and enter 4 and it would do the same thing.  Obviously this program is a lot more complex so this would help a ton.  Thanks a bunch.

 

%macro repeat (rpt_mnth, yr);
data SCCU_Member_data_&rpt_mnth._&rpt_yr.;
set in.SCCU_Member_data_&rpt_mnth._&rpt_yr.;
run;
data SNME_Member_data_&rpt_mnth._&rpt_yr.;
set in.SNME_Member_data_&rpt_mnth._&rpt_yr.;
run;
%mend repeat;
%repeat(1, 2020); /*January*/
%repeat(2, 2020); /*February*/
%repeat(3, 2020); /*March*/
%repeat(4, 2020); /*April*/
%repeat(5, 2020); /*May*/
%repeat(6, 2020); /*June*/
%repeat(7, 2020); /*July*/
%repeat(8, 2020); /*August*/
%repeat(9, 2020); /*September*/
%repeat(10, 2020); /*October*/
%repeat(11, 2020); /*November*/
%repeat(12, 2020); /*December*/

 

 

data final_sccu_member;
set Sccu_member_data_1_2020 Sccu_member_data_2_2020 Sccu_member_data_3_2020;
run;
data final_snme_member;
set Snme_member_data_1_2020 Snme_member_data_2_2020 Snme_member_data_3_2020;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
unison
Lapis Lazuli | Level 10

Try -- adjust macro to reflect your process. Requires no update in terms of dates since it is based on SYSDATE (today):

*Macro;
%macro repeat(rpt_mnth,rpt_yr);
	data SCCU_Member_data_&rpt_mnth._&rpt_yr.;
		format message $50.;
		message=catx('','SCCU Data for Rpt_Month:',"&rpt_mnth",'Rpt_Year:',"&rpt_yr");
	run;
	data SNME_Member_data_&rpt_mnth._&rpt_yr.;
		format message $50.;
		message=catx('','SNME Data for Rpt_Month:',"&rpt_mnth",'Rpt_Year:',"&rpt_yr");
	run;
%mend;


*Run based on SYSDATE=TODAY;
%let mth=%sysfunc(month("&sysdate"d));
%let yr=%sysfunc(year("&sysdate"d));
%put &=mth &=yr;

data _null_;
	do i=1 to &mth-1;
		call execute(cats('%repeat(rpt_mnth=',i,',rpt_yr=',"&yr",')'));
	end;
run;

data final_sccu_member;
	set sccu_member_data_:;
run;

data final_snme_member;
	set snme_member_data_:;
run;

proc print data=final_sccu_member;
run;
proc print data=final_snme_member;
run;
-unison

View solution in original post

5 REPLIES 5
DavePrinsloo
Pyrite | Level 9

Not sure if this is all you need:

 

%macro control(Maxmonth=, year=);
%local mi;
%do mi=1 %to &maxmonth.;
    %repeat (&mi, &year.)
%end; /* of %di mi */

data final_sccu_member;
set 
%do mi=1 %to %do mi=1 %to &maxmonth.;
    Sccu_member_data_&mi._&year.
%end;
; run; data final_snme_member; set %do mi=1 %to %do mi=1 %to &maxmonth.; Snme_member_data_&mi._&year. %end;
; run; %mend control; /* call the control macro */ %control(maxmonth=3, year=2020);

 

 

 

unison
Lapis Lazuli | Level 10

Try -- adjust macro to reflect your process. Requires no update in terms of dates since it is based on SYSDATE (today):

*Macro;
%macro repeat(rpt_mnth,rpt_yr);
	data SCCU_Member_data_&rpt_mnth._&rpt_yr.;
		format message $50.;
		message=catx('','SCCU Data for Rpt_Month:',"&rpt_mnth",'Rpt_Year:',"&rpt_yr");
	run;
	data SNME_Member_data_&rpt_mnth._&rpt_yr.;
		format message $50.;
		message=catx('','SNME Data for Rpt_Month:',"&rpt_mnth",'Rpt_Year:',"&rpt_yr");
	run;
%mend;


*Run based on SYSDATE=TODAY;
%let mth=%sysfunc(month("&sysdate"d));
%let yr=%sysfunc(year("&sysdate"d));
%put &=mth &=yr;

data _null_;
	do i=1 to &mth-1;
		call execute(cats('%repeat(rpt_mnth=',i,',rpt_yr=',"&yr",')'));
	end;
run;

data final_sccu_member;
	set sccu_member_data_:;
run;

data final_snme_member;
	set snme_member_data_:;
run;

proc print data=final_sccu_member;
run;
proc print data=final_snme_member;
run;
-unison
cjac45
Fluorite | Level 6

Thank you this was a great suggestion! 🙂  Appreciate all the help!

andreas_lds
Jade | Level 19

Creating datasets for each month seems to be the first problem to be addressed. Having in.SCCU_Member_data with one date variable (formatted to display only month and year) would make all the joining redundant, same change for in.SNME_Member_data.

 

If you can't apply that bigger change, try

data _null_;  
   months = intck('month', '01Jan2020'd, today());

   do i = 0 to months-1;
      date = intnx('month', '01Jan2020'd, i);
      call execute(cats('%nrstr(%repeat(', month(date), ',', year(date), '));'));
   end;
run;


data final_sccu_member;
   set Sccu_member_data_:;
run;
data final_snme_member;
   set Snme_member_data:;
run;
ballardw
Super User

If the data sets were named in YEAR_month order then, at least within a single year, this is much easier and no macro may be needed. You don't show any call for crossing a calendar year boundary so I'm not sure how valid that is

 

Regardless there is absolutely no need to copy the data set as you show unless you are doing other calculations.

 

data junk_2020_1;
   set sashelp.class;
run;
data junk_2020_2;
   set sashelp.class;
run;
data junk_2020_3;
   set sashelp.class;
run;
data junk_2020_4;
   set sashelp.class;
run;
%let mnth=3;
data want;
   set junk_2020_1 - junk_2020_&mnth.
   ;
run;

Which of course will fail if mnth references a data set that doesn't  yet exist.

If you are doing calculations in each of those copied sets very likely you could move them to the step that combines the data.

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 881 views
  • 0 likes
  • 5 in conversation