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

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!!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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.

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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";

 

Astounding
PROC Star

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.

SteelersPitts
Obsidian | Level 7

You're a rockstar! Thanks!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 3 replies
  • 2470 views
  • 2 likes
  • 3 in conversation