DATA Step, Macro, Functions and more

Can you help me "Macro-tize" my code?

Accepted Solution Solved
Reply
Contributor
Posts: 62
Accepted Solution

Can you help me "Macro-tize" my code?

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!


Accepted Solutions
Solution
‎04-03-2014 05:25 AM
Super User
Super User
Posts: 7,977

Re: Can you help me "Macro-tize" my code?

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


All Replies
Super User
Posts: 19,850

Re: Can you help me "Macro-tize" my code?

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

Super User
Super User
Posts: 7,070

Re: Can you help me "Macro-tize" my code?

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)

Contributor
Posts: 62

Re: Can you help me "Macro-tize" my code?

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.

Solution
‎04-03-2014 05:25 AM
Super User
Super User
Posts: 7,977

Re: Can you help me "Macro-tize" my code?

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;


🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 351 views
  • 6 likes
  • 4 in conversation