If you wish to succeed, you should use persistence as your good friend, experience as your reference, prudence as your brother and hope as your sentry.------如果你希望成功,当以恒心为良友、以经验为参谋、以谨慎为兄弟、以希望为哨兵。
reference: "SAS® Code and Macros: How They Interact" write byBruce Gilsen, Federal Reserve Board from support.sas.com every time you get the step boundary, the code start compile and execute; %macro to %mend the macro finish the compile and stored in the catalog in a library, when then macro finish the execute, it can be treated as the code user has typed between the other code; The macro will be execute when you invokes it or in the step boundary; eg. %macro mac5; (1) %local j; (2) %do j = 1 %to 3; (3) xj = yj (4) %end; (5) %mend mac5; (6) data two; (7) set one; (8) rename (9) %mac5 (10) ; (11) run; (12) to (6) , then macro finish the compile, then to(12), it start execute, then (10) will become the text just like x1=y1 x2=y2 x3=y3; then execute the data step; then we talk about the call execute to submit SAS code or macro code in a DATA step; data one; input obs company $ audited; cards; 1 ibm 1 2 microsoft 0 3 ao1 1 ; run; %macro mac7(firm=); %put in mac7 firm= firm; %mend mac7; data two; set one; where audited = 1; call execute ( '%mac7(firm= '||company||')' ); put "at end of TWO " company=; run; when you execute this data step, when to the call execute, then execute the macro at once, then it can be a text between the code; %macro mac8(firm=, vars=); %put in mac8 firm= firm vars= vars; proc print data= firm; title firm; var vars; run; %mend mac8; data two; set one; if audited = 1 then call execute ( '%mac8(firm='||company||', vars=income tax)' ); else call execute ( '%mac8(firm='||company||', vars=income)' ); put "at end of TWO " company=; run; be caution that the proc print will be executed after the data step has finished; %macro mac9 ( state= ); (1) proc summary data = all_state_sales; (2) var sales; (3) where state1="state"; (4) output out = summary1 max=maxsale1; (5) run; (6) data _null_; (7) set summary1; (8) call symputx ( 'maxsale' , maxsale1 ); (9) run; (10) %if maxsale 200 %then %do; (11) title "Maximum Sale in state is maxsale"; (12) proc print data = all_state_sales; (13) where state1="state"; (14) run; (15) %end; (16) %mend mac9; (17) data _null_; (18) input state1 $2.; (19) call execute ( '%nrstr(%mac9(state=' || state1 ||'))' ); datalines; (21) CA NY ;run; when you just use statement like 'call execute ( '%mac9(state=' || state1 ||')' ), the sas will execute the macro and the first two data step will execute aftert the data step and then execute %if at first and then you will see syntax error, because the macro variable maxsale has no value yet, but 'call execute ( '%nrstr(%mac9(state=' || state1 ||'))' ) just like add %mac9(...) after this data step, and it will execute then macro directly and the problem can bu resolved. So, it is important to add %nrstr before the %macro(variable='||..||')