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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

7 REPLIES 7
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10

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.

Steelers_In_DC
Barite | Level 11

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;

Astounding
PROC Star

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.

Steelers_In_DC
Barite | Level 11

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

Tom
Super User Tom
Super User

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;

Steelers_In_DC
Barite | Level 11

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;

Ron_MacroMaven
Lapis Lazuli | Level 10

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

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
  • 7 replies
  • 1275 views
  • 0 likes
  • 5 in conversation