DATA Step, Macro, Functions and more

Do loop within a set statement

Reply
Occasional Contributor
Posts: 15

Do loop within a set statement

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

 

Super User
Super User
Posts: 9,789

Re: Do loop within a set statement

Posted in reply to DeclanBall

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.

Super User
Posts: 8,213

Re: Do loop within a set statement

Posted in reply to DeclanBall

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

 

Art, CEO, AnalystFinder.com

 

Occasional Contributor
Posts: 15

Re: Do loop within a set statement

8.02.02MOP020601
Super User
Super User
Posts: 9,789

Re: Do loop within a set statement

Posted in reply to DeclanBall

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.  

Super User
Posts: 8,213

Re: Do loop within a set statement

Posted in reply to DeclanBall

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

 

Ask a Question
Discussion stats
  • 5 replies
  • 155 views
  • 0 likes
  • 3 in conversation