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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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