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

Hi SAS friends,

I have some code I've written using data-step but after many hours of troubleshooting i can't get it to work in Macro language. can you 'convert' it for me?

%macro Get_DataSets(date,date2);

  %put &date.;

  %put &date2.;

%mend Get_DataSets;

data dates;

length date2 $8.;

  do i=1 to 9;

   date=intnx('month','01MAY2013'd,i);

    year=put(year(date),$4.);

  month=put(month(date),z2.);

  date2=compress(year||"_"||month);

  %Get_DataSets(date,date2);

   output;

  end;

  format date monyy.;

  keep date date2;

run;

i  wanna call a macro within a macro but I can't figure out how to send my two macro variables to %Get_DataSets

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Not sure what you are trying to achieve however the below produces the list of put statements.:

%macro Get_DataSets(date,date2);
  %put &date.;
  %put &date2.;
%mend Get_DataSets;

data dates;
  length date 8. date2 $8.;
  do i=1 to 9;
    date=intnx('month','01MAY2013'd,i);
    date2=put(year(date),4.)||"_"||put(month(date),z2.);
    call execute('%Get_DataSets('||put(date,mmyy.)||','||strip(date2)||');');
  end;
run;


View solution in original post

4 REPLIES 4
Reeza
Super User

Look up call execute to replace your %get_data in the middle of your data step. 

Tom
Super User Tom
Super User

You are not calling a macro within a macro, you are attempting perhaps to call a macro inside a data step, but that normally cannot work.

Looks like what you want is to generate calls to a macro based on values in a data set.

You can use the new CAT type functions to generate the macro call.

  cmd = cats('%nrstr(%get_data)(',date,',',date2,')') ;

To execute the generated macro calls you can use CALL EXECUTE.

    call execute(cmd);

Or you can just use PUT statements to write out the macro calls and then %INC the generated code.

filename code temp;

....

file code ;

put '%get_data(' date ',' date2 ')' ;

run;

%inc code / source2 ;

Or perhaps you just want to understand how to loop by month in a macro %DO loop?

%macro outer(start);

  %local i date year month date2 ;

  %do i=1 to 9 ;

    %let date = %sysfunc(intnx(month,&start,&i)) ;

    %let year = %sysfunc(year(&date),4.)) ;

    %let month = %sysfunc(month(&date),z2.);

    %get_datasets(&date,&year._&month);

  %end;

%mend outer;

%outer('01MAY2013'd)

SAShole
Pyrite | Level 9

RW9,

that's exactly what i was looking for. Thanks!

Tom,

Thank you for going above and beyond and showing me a few approaches to solve this. much appreciated.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Not sure what you are trying to achieve however the below produces the list of put statements.:

%macro Get_DataSets(date,date2);
  %put &date.;
  %put &date2.;
%mend Get_DataSets;

data dates;
  length date 8. date2 $8.;
  do i=1 to 9;
    date=intnx('month','01MAY2013'd,i);
    date2=put(year(date),4.)||"_"||put(month(date),z2.);
    call execute('%Get_DataSets('||put(date,mmyy.)||','||strip(date2)||');');
  end;
run;


hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 4 replies
  • 1959 views
  • 6 likes
  • 4 in conversation