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

I have a number of datasets of the form:

 

example_201812

example_201901

...

example 202008

 

I would like to create a macro where the argument is a starting month of the form eg 201812, and the macro loops over a 12 month period starting with the starting month (eg 201812 to 201911). I have something like this in mind:

 

%MACRO year(YYYYMM);

%do j = 0 %to 11;
	%let date_&j. = intnx('months',&YYYYMM.,&j.);

  	%if date_&j.= &YYYYMM. %then %do; /*First month*/

 	proc sql;
	create table WANT_&j. as select
	*
	from example_&date_&j.;
	run;

	%end;
	%else %do; /* Remaining months */

 	proc sql;
	create table WANT_&j. as select
	*
	from example_&date_&j.;
	run;

  	%end;

%MEND;

Any help would be greatly appreciated

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Tip: work with actual SAS date values so that you can use built in SAS functions and built in SAS formats, instead of character strings which you then have to pull apart yourself.

 

Partial example (UNTESTED)

 

%macro dothis;
%let start_date=%sysevalf('01DEC2018'd);
%do j=0 %to 11;
     %let thismonth=%sysfunc(intnx(month,&start_date,&j,b);
      proc sql;
                ... whatever ...
                from example_%sysfunc(putn(&thismonth,yymmn6.));
       quit;
%end;
%mend;
%dothis

Now, macro variable &START_DATE is an actual SAS date value (it equals 21519, which is the number of days since 01JAN1960) and then everything works smoothly using SAS date functions (INTNX) and SAS date formats (YYMMN6.)

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Tip: work with actual SAS date values so that you can use built in SAS functions and built in SAS formats, instead of character strings which you then have to pull apart yourself.

 

Partial example (UNTESTED)

 

%macro dothis;
%let start_date=%sysevalf('01DEC2018'd);
%do j=0 %to 11;
     %let thismonth=%sysfunc(intnx(month,&start_date,&j,b);
      proc sql;
                ... whatever ...
                from example_%sysfunc(putn(&thismonth,yymmn6.));
       quit;
%end;
%mend;
%dothis

Now, macro variable &START_DATE is an actual SAS date value (it equals 21519, which is the number of days since 01JAN1960) and then everything works smoothly using SAS date functions (INTNX) and SAS date formats (YYMMN6.)

--
Paige Miller
ChristianWI
Fluorite | Level 6

Wow, perfect! Thanks!

Reeza
Super User

And just a note that if you're trying to append these data sets or combine them somehow, you can use shortcut references.

 

data combined;
set example_2018:  example_2019: ;
run;

This will append all data sets that start with example_2018 or example_2019. 

 

Here is a reference that illustrates how to refer to variables and datasets in a short cut list:
https://blogs.sas.com/content/iml/2018/05/29/6-easy-ways-to-specify-a-list-of-variables-in-sas.html

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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