12-12-2017 09:24 AM
I am really struggling with coding a macro. I have a library of datasets in the format:
I would like the macro to take a year and month input and create a dataset that sets 3 years worth of datasets starting one month prior. For example:
%macro threeyears(16,May) ; would create a dataset containing April 2016 back to May 2013 (36 months).
I am also unfortunately working with an "ancient" version of SAS and therefore do not have certain functions/
Any help is much appreciated,
12-12-2017 09:33 AM
This doesn't make sense "dataset that sets 3 years worth of datasets" - put all related data together and keep it that way.
Anyways, simplest method - get all the data you want in one area:
data want; length ds_name $20; set jan: feb: mar: ... indsname=tmp; ds_name=indsname; run;
This will set all datasets with jan prefix, all with feb prefix etc. The ds_name will contain the string of the name of the dataset which was read in and you can then process that string to get year and other information out, then simply where clause for year= what you want.
12-12-2017 10:50 AM
I agree with @RW9 that it would be a lot easier if you upgrade your SAS version. However, that said, I think you can use something like the following with version 8:
data jangroup14 jangroup15 jangroup16 febgroup14 febgroup15 febgroup16 margroup14 margroup15 margroup16; input x; cards; 1 2 ; %macro import(month); data &month; set &month.group14 (in=_14) &month.group15 (in=_15) &month.group16 (in=_16) ; format date date9.; if _14 then date=input('01'||"&month"||'2014',date9.); else if _15 then date=input('01'||"&month"||'2015',date9.); else if _16 then date=input('01'||"&month"||'2016',date9.); run; %mend import; %import(jan) %import(feb) %import(mar) data want; set jan feb mar; run;
Of course, expand that to include all of your data and then simply use an if statement to get the subset that you want/need.
Art, CEO, AnalystFinder.com