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!
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;
Look up call execute to replace your %get_data in the middle of your data step.
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)
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.
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.