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;


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