DATA Step, Macro, Functions and more

macro variable in macro

Accepted Solution Solved
Reply
Valued Guide
Posts: 860
Accepted Solution

macro variable in macro

I would like to build a macro variable within a macro.  The following doesn't work but I'm hoping it will give you some idea of what I'm trying to accomplish.  The call symput lines are where my question is.  How do I go about accomplishing this in a loop?  Is it possible to change the libname and then change the data source each iteration?  It's easy enough to set up 12 macro variables prior to this step but I was hoping to avoid doing that.

%macro cib;

%do i= &num_month %to 01 %by -1;

call symputx('lastmon',substr(put(intnx('month',date9,-&i,'e'),date9.),3,3),'g');

call symputx('monyear',substr(put(intnx('month',date9,-&i,'e'),mmddyy10.),1,2)||substr(put(intnx('month',date9,-&i,'e'),date9.),6,4),'g');

libname &lastmon "/sas/data/execution/ccmr/&lastmon";

data cust_inst_base_&monyear;

set &lastmon..cust_inst_base_&monyear;

run;

%end;

;

run;

%mend;

%cib;

Thank You in advance,

Mark

Go Pirates

Message was edited by: Mark Johnson


Accepted Solutions
Solution
‎07-10-2015 02:47 PM
Super User
Posts: 5,500

Re: macro variable in macro

Posted in reply to Steelers_In_DC

Mark,

CALL SYMPUT is actually a DATA step statement.  The quickest fix might be to insert two lines into your code:

data _null_;

call symputx('lastmon',substr(put(intnx('month',date9,-&i,'e'),date9.),3,3),'g');

call symputx('monyear',substr(put(intnx('month',date9,-&i,'e'),mmddyy10.),1,2)||substr(put(intnx('month',date9,-&i,'e'),date9.),6,4),'g');

run;

No guarantees that this handles all the problems, but it moves you significantly in the right direction.

Good luck.

View solution in original post


All Replies
Super Contributor
Super Contributor
Posts: 3,174

Re: macro variable in macro

Posted in reply to Steelers_In_DC

You will wan to explain where you assign macro variable &num_month and also SAS dataset variable date9 -- this is not revealed with what has been contributed.

Likely you might consider using CALL EXECUTE within a DATA step that contains your iteration variable(s) and do so with a DATA step DO/END loop.

Always recommend starting with "stick figures" and then work up to building your solution -- that would be to use the very basic idea of instream DATALINES to compose a prototype of the working parts, then substitute in your processing variables.

Scott Barry

SBBWorks, Inc.

Valued Guide
Posts: 860

Re: macro variable in macro

The other variables come from this step:

data _null_;

date9 = today()-15; /* add -15 after today() if testing before the 15th business day*/

call symput('num_month',strip(put(month(today())-2,z2.))); /*-1 should be changed to -2 if testing before the 15th business day*/

call symputx('lastmon',substr(put(intnx('month',date9,-1,'e'),date9.),3,3),'g');

call symputx('monyear',substr(put(intnx('month',date9,-1,'e'),mmddyy10.),1,2)||substr(put(intnx('month',date9,-1,'e'),date9.),6,4),'g');

run;

Solution
‎07-10-2015 02:47 PM
Super User
Posts: 5,500

Re: macro variable in macro

Posted in reply to Steelers_In_DC

Mark,

CALL SYMPUT is actually a DATA step statement.  The quickest fix might be to insert two lines into your code:

data _null_;

call symputx('lastmon',substr(put(intnx('month',date9,-&i,'e'),date9.),3,3),'g');

call symputx('monyear',substr(put(intnx('month',date9,-&i,'e'),mmddyy10.),1,2)||substr(put(intnx('month',date9,-&i,'e'),date9.),6,4),'g');

run;

No guarantees that this handles all the problems, but it moves you significantly in the right direction.

Good luck.

Valued Guide
Posts: 860

Re: macro variable in macro

Posted in reply to Astounding

That did fix that aspect of the problem, thanks.  I'll probably be back before I finish this step.

Super User
Super User
Posts: 7,039

Re: macro variable in macro

Posted in reply to Steelers_In_DC

You probably do not need to modify the LIBREF since you are changing the path that it points to.  Also that way you do not need be sure that the value of &LASTMON is valid to use as a libref.

libname lastmon "/sas/data/execution/ccmr/&lastmon";

data cust_inst_base_&monyear;

set lastmon.cust_inst_base_&monyear;

Valued Guide
Posts: 860

Re: macro variable in macro

Tom, that is true and a good point.  It's a personal preference for me.  The way the code and log will be documented I like to see the applicable month for each libname.  Here's the final code in case it helps anyone in the future.  Do people search here first?  Smiley Happy

data _null_;

date9 = today()-15; /* add -15 after today() if testing before the 15th business day*/

call symput('num_month',strip(put(month(today())-2,z2.))); /*-1 should be changed to -2 if testing before the 15th business day*/

run;

%macro cib;

%do i= &num_month %to 01 %by -1;

data _null_;

    date9 = today()-15; /* add -15 after today() if testing before the 15th business day*/

    call symput('num_month',strip(put(month(today())-&i-1,z2.))); /*-1 should be changed to -2 if testing before the 15th business day*/

    call symputx('year',strip(year(today())),'g');

    call symputx('date9',put(date9,date9.),'g');

    call symputx('gooddate9',put(intnx('month',date9,-&i,'e'),date9.),'g');

    call symputx('date9_2',put(intnx('month',date9,-&i,'e'),date9.),'g');

    call symputx('lastmon',substr(put(intnx('month',date9,-&i,'e'),date9.),3,3),'g');

    call symputx('mon',put(intnx('month',date9,-&i,'e'),date9.),'g');

    call symputx('_mon',put(intnx('month',date9,-&i-1,'e'),date9.),'g');

    call symputx('_lastmon',substr(put(intnx('month',date9,-&i-1,'e'),date9.),3,3),'g');

    call symputx('monyear',substr(put(intnx('month',date9,-&i,'e'),mmddyy10.),1,2)||substr(put(intnx('month',date9,-&i,'e'),date9.),6,4),'g');

    call symputx('_monyear',substr(put(intnx('month',date9,-&i,'e'),mmddyy10.),1,2)||substr(put(intnx('month',date9,-&i,'e'),date9.),6,4),'g');

    call symputx('mmddyy10',put(intnx('month',date9,-&i,'e'),mmddyy10.),'g');

    call symputx('_mmddyy10',put(intnx('month',date9,-&i,'e'),mmddyy10.),'g');

run;

data _null_;

    call symputx('propdate',cats(propcase(substr("&mon",3,3)),substr("&mon",6,4)),'g');

    call symputx('_propdate',cats(propcase(substr("&_mon",3,3)),substr("&_mon",6,4)),'g');

run;

data test_&monyear;

    test = "&lastmon";

run;

;

%end;

%mend;

%cib;

Regular Contributor
Posts: 227

Re: macro variable in macro

Posted in reply to Steelers_In_DC

Most of these date-stamp assignments can be done with macro assignment statements,

i.e. no need to run a data null with symput.

On this page

http://www.sascommunity.org/wiki/Macro_Loops_with_Dates

see

🔒 This topic is solved and locked.

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

Discussion stats
  • 7 replies
  • 376 views
  • 0 likes
  • 5 in conversation