I'm trying to pass file name to a macro. The macro runs once a month, therefore, I'm trying to store the output file with a month prefix. In the current code someone has to manually provide a file name every month (Sep17_Sales, Oct17_Sales etc.). I want to automate this so that SAS generates files with the name of the month prefixed to the data file.
Macro:
%macro sales (outdata = , dt =);
Current Code
%Sales(Outdata = Sep17_Sales, dt = '2017-09-01');
%Sales(Outdata =Oct17_Sales, dt ='2017-10-01');
My approach:
data _null_;
current_date = today();
current_month = intnx('month', current_date, 0, "Beginning");
Name = "_Sales";
Result = put(current_month, monyy7.) || name;
run;
%Sales(Outdata=Result, dt='2017-10-01');
When I try to pass the parameter, it throws error. I tried changing Result to %Let Result and pass a reference &Result to the macro but it also fails.
Any suggestion how to solve this? Thank you for all the help!!
You've gotten most of the way there. But as you have seen, macro calls do not use DATA step variables. Here is a way to bridge that gap.
First, finish your DATA step with:
Result = put(current_month, monyy7.) || name;
call symputx('result_value', result);
run;
Then call the macro with:
%Sales(Outdata=&result_value, dt='2017-10-01')
There are many ways to get to that same end result. This one utilizes what you have done so far, while making very few changes.
Note that it might be possible to automatically generate the second parameter in similar fashion.
Well beginning is spelt incorrectly to start with.
Anyways, in your macro can you not just do:
filename "<pathtofile>\%sysfunc(put(month(today(),monname3.)))_rest_of_name.txt";
You've gotten most of the way there. But as you have seen, macro calls do not use DATA step variables. Here is a way to bridge that gap.
First, finish your DATA step with:
Result = put(current_month, monyy7.) || name;
call symputx('result_value', result);
run;
Then call the macro with:
%Sales(Outdata=&result_value, dt='2017-10-01')
There are many ways to get to that same end result. This one utilizes what you have done so far, while making very few changes.
Note that it might be possible to automatically generate the second parameter in similar fashion.
You're a rockstar! Thanks!
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.