DATA Step, Macro, Functions and more

Pass Character Parameter Dynamically to a Macro

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

Pass Character Parameter Dynamically to a Macro

[ Edited ]

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

 

 


Accepted Solutions
Solution
‎11-07-2017 01:25 PM
Super User
Posts: 6,934

Re: Pass Character Parameter Dynamically to a Macro

[ Edited ]
Posted in reply to SteelersPitts

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


All Replies
Super User
Super User
Posts: 9,842

Re: Pass Character Parameter Dynamically to a Macro

Posted in reply to SteelersPitts

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

 

Solution
‎11-07-2017 01:25 PM
Super User
Posts: 6,934

Re: Pass Character Parameter Dynamically to a Macro

[ Edited ]
Posted in reply to SteelersPitts

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.

Contributor
Posts: 24

Re: Pass Character Parameter Dynamically to a Macro

Posted in reply to Astounding

You're a rockstar! Thanks!

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 179 views
  • 2 likes
  • 3 in conversation