BookmarkSubscribeRSS Feed
DeclanBall
Fluorite | Level 6

Hello,

 

I am really struggling with coding a macro. I have a library of datasets in the format:

 

JanGroup15

FebGroup15

..

..

JanGroup16

FebGroup16

etc.

 

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,

Declan

 

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

art297
Opal | Level 21

You said ancient! Which version of SAS are you using?

 

Art, CEO, AnalystFinder.com

 

DeclanBall
Fluorite | Level 6
8.02.02MOP020601
RW9
Diamond | Level 26 RW9
Diamond | Level 26

But you can't get the monoliths anymore?

 

Seriously, unless you have a dodgy copy, move to v9 at minimum, 9.4 latest release highly recommended.  

art297
Opal | Level 21

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

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 863 views
  • 0 likes
  • 3 in conversation