BookmarkSubscribeRSS Feed
claforest1
Fluorite | Level 6

Hi - I am very much a beginner at SAS, so would like to apologize ahead of time for a (probably) simple question!

 

I am creating a table pulling one line of data per dataset I am using. The datasets all have the format loanreport_YYYYMM. I need years 2010 through 2019 and months 01, 04, 07, and 10 for all years. For example, I need loanreport_201901, loanreport_201904, loanreport_201907, etc. I currently have this basic macro and it works fine, but I am looking for a way I can automate it more by being able to just change the 'end date' at the top of the code with a macro. This program is run once a year and when it is run, only the '07' month is ready for the current year. 

 

So for 2020, I would like to be able to change end date to 2020 and have it pull the 201910, 202001, 202004, and 202007 datasets to create those 4 new lines in the proc sql table. I know this is possible and have read other posts similar to this, but I just can't figure out how to make it work for my code. Thank you!!

 

 

%macro portfolio(date_input, month);

data example_&date_input.&month.;
	set loanreport_&date_input.&month. (keep =	variables variables);
run;

proc sql;
	create table alltheloans_&date_input.&month. as select
	&date_input.&month. as dataasof,
	other variables, 
        other variables,
from example_&date_input.&month.
	group by dataasof;
quit;

%mend;
%portfolio_(2010, 10);
%portfolio_(2011, 01);
%portfolio_(2011, 04);
%portfolio_(2011, 07);
%portfolio_(2011, 10);
etc. etc. through %portfolio_(2019, 07) ;

data home.portfolio_7a;
	set	home.portfolio_201010
		home.portfolio_201101
		home.portfolio_201104
		home.portfolio_201107
		home.portfolio_201110
etc. etc. etc. 
run  

 

 

1 REPLY 1
PaigeMiller
Diamond | Level 26

Looping involving calendar dates in a macro is much easier if you work with SAS date values (and not human readable values such as 2010 and 10). Therefore, I convert 2010 and 10 to an actual SAS data value using the MDY function. Then you can use the INTNX and INTCK functions as needed to work in terms of quarters (or months or weeks, if desired).

 

%macro loop(startyear,startmonth);
    %let date=%sysfunc(mdy(&startmonth,1,&startyear));
	%do %while(&date<%sysfunc(today()));
/*		%put %sysfunc(putn(&date,yymm7.)) %sysfunc(year(&date)) %sysfunc(month(&date));*/
	    %portfolio_(%sysfunc(year(&date)),%sysfunc(month(&date)))
	    %let date=%sysfunc(intnx(quarter,&date,1,b));
	%end;
%mend;
%loop(2010,10)

You would probably also be wise to re-configure this code so that you are always working with actual valid SAS date values, rather than discrete year and discrete month used as &date_input.&month. A SAS date value can be formatted and used in file names as 201907, rather than having to append discrete year and discrete month onto the end a file name.

--
Paige Miller

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 680 views
  • 0 likes
  • 2 in conversation