SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14
Hello
I want to run macro for different macro values that are taken from data set.
Way3 is not working (error)
what is the way to fix it please???
 
 


data t2410;
input CustID x;
cards;
1 10
2 20
;
Run;
data t2411;
input CustID w;
cards;
1 15
2 25
;
Run;
data t2412;
input CustID z;
cards;
1 30
2 50
;
Run;
data t2501;
input CustID y;
cards;
1 1
2 0
;
Run;
data today_tbl;
input CustID x_today w_today z_today y_today;
cards;
1 10 20 30 1
2 20 30 40 0
;
Run;

/**Way1 to run---type manual the run code***/
/**Way1 to run---type manual the run code***/
/**Way1 to run---type manual the run code***/
%macro RRR(mon);
data want&mon.;
merge t&mon. today_tbl;
by CustID;
Run;
%mend RRR;
%RRR(2410)
%RRR(2411)
%RRR(2412)
%RRR(2501)


/**Way2 to run***/
/**Way2 to run***/
/**Way2 to run***/
%let start_YYMM=2410;
%let end_YYMM=2501;
%let date_start=%sysfunc(inputn(&start_YYMM.,yymmn4.));
%let date_end=%sysfunc(inputn(&end_YYMM.,yymmn4.));
%let nr_months=%SYSFUNC(intck(month,&date_start,&date_end));
%put date_start=&date_start. ;
%put date_end=&date_end. ;
%put nr_months=&nr_months. ;

%macro months;
%do j=0 %to &nr_months;
YYMM=input(put(intnx('month',&date_start.,&j.),yymmn4.),best.);
output;
%end;
%mend months;

data series_YYMM_for_macro_tbl;
%months;
run;

proc sql;
create table Run_macro_code_tbl as
select cats('%RRR(',YYMM,')') as Run_code
from series_YYMM_for_macro_tbl
;
quit;

proc sql noprint;
select Run_code into :code separated by ' '
from Run_macro_code_tbl
;
quit;
&code

/**Way3 to run***/
/**Way3 to run***/
/**Way3 to run***/
options mprint;/* turn on macro debugging**/

data _null_;
Set Run_macro_code_tbl;
call execute(cats('%nrstr(Run_code'));
Run;
 
 
 
Log of way3 that have error
1 The SAS System 11:26 Wednesday, April 23, 2025

1 ;*';*";*/;quit;run;
2 OPTIONS PAGENO=MIN;
3 %LET _CLIENTTASKLABEL='Program 1';
4 %LET _CLIENTPROCESSFLOWNAME='Standalone Not In Project';
5 %LET _CLIENTPROJECTPATH='';
6 %LET _CLIENTPROJECTPATHHOST='';
7 %LET _CLIENTPROJECTNAME='';
8 %LET _SASPROGRAMFILE='';
9 %LET _SASPROGRAMFILEHOST='';
10
11 ODS _ALL_ CLOSE;
12 OPTIONS DEV=SVG;
13 GOPTIONS XPIXELS=0 YPIXELS=0;
14 %macro HTML5AccessibleGraphSupported;
15 %if %_SAS_VERCOMP_FV(9,4,4, 0,0,0) >= 0 %then ACCESSIBLE_GRAPH;
16 %mend;
17 FILENAME EGHTML TEMP;
18 ODS HTML5(ID=EGHTML) FILE=EGHTML
19 OPTIONS(BITMAP_MODE='INLINE')
20 %HTML5AccessibleGraphSupported
NOTE: The ACCESSIBLE_GRAPH option is pre-production for this release.
21 ENCODING='utf-8'
22 STYLE=HTMLBlue
23 NOGTITLE
24 NOGFOOTNOTE
25 GPATH=&sasworklocation
26 ;
NOTE: Writing HTML5(EGHTML) Body file: EGHTML
27
28 data _null_;
29 call execute(cats('%nrstr(Run_code'));
30 Run;

ERROR: Expected close parenthesis after macro function invocation not found.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
user cpu time 0.00 seconds
system cpu time 0.00 seconds
memory 347.71k
OS Memory 26524.00k
Timestamp 04/23/2025 06:34:58 PM
Step Count 231 Switch Count 0
Page Faults 0
Page Reclaims 42
Page Swaps 0
Voluntary Context Switches 0
Involuntary Context Switches 0
Block Input Operations 0
Block Output Operations 0


NOTE: CALL EXECUTE generated line.
1 + Run_code
31
32
33 %LET _CLIENTTASKLABEL=;
34 %LET _CLIENTPROCESSFLOWNAME=;
2 The SAS System 11:26 Wednesday, April 23, 2025

35 %LET _CLIENTPROJECTPATH=;
36 %LET _CLIENTPROJECTPATHHOST=;
37 %LET _CLIENTPROJECTNAME=;
38 %LET _SASPROGRAMFILE=;
39 %LET _SASPROGRAMFILEHOST=;
40
41 ;*';*";*/;quit;run;
NOTE: Line generated by the CALL EXECUTE routine.
1 + Run_code
________
180
ERROR 180-322: Statement is not valid or it is used out of proper order.

42 ODS _ALL_ CLOSE;
43
44
45 QUIT; RUN;
46
1 REPLY 1
Tom
Super User Tom
Super User

The error is easy to see from the log:

29         call execute(cats('%nrstr(Run_code'));
30         Run;

ERROR: Expected close parenthesis after macro function invocation not found.

You do not close the %NRSTR() function call. 

But what is Run_code??  That is not a valid SAS command. And it does not have either % or & so it has nothing to do with the macro processor either. And it is not a variable in the data step, since there are no variables in that data step. 

 

Don't you want to run the macro RRR?

%RRR(2410)
%RRR(2411)
%RRR(2412)
%RRR(2501)

So just do that.

data _null_;
  start='01OCT2024'd;
  end='01JAN2025'd;
  do offset=0 to intck('month',start,end);
    call execute(cats('%nrstr(%rrr)(',put(intnx('month',start,offset),yymmn4.),')'));
  end;
run;

Example:

1    %macro rrr / parmbuff;
2    %put Running macro call &sysmacroname.&syspbuff.;
3    %mend rrr;
4    data _null_;
5      start='01OCT2024'd;
6      end='01JAN2025'd;
7      do offset=0 to intck('month',start,end);
8        call
8  ! execute(cats('%nrstr(%rrr)(',put(intnx('month',start,offset),yymmn4.),')'
8  ! ));
9      end;
10   run;


NOTE: CALL EXECUTE generated line.
1   + %rrr(2410)
Running macro call RRR(2410)
2   + %rrr(2411)
Running macro call RRR(2411)
3   + %rrr(2412)
Running macro call RRR(2412)
4   + %rrr(2501)
Running macro call RRR(2501)

 

 

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 195 views
  • 1 like
  • 2 in conversation