I have two codes
Code-1
%macro Zero (Input=, Output=);
data &Output.;
set &Input.;
array change _numeric_;
do over change;
if change=. then change=0;
end;
run ;
%mend;
Code-2
%macro Zero (Input=, Output=);
data &Output.;
set &Input.;
array change _numeric_;
%do %over change;
%if change=. %then change=0;
%end;
run ;
%mend;
the only difference is % sign with loop in code-2. I get the same output.
Question is:
1) Why and where % sign is require for macro.
2) if the output is same in both the query why do we need % sign for loop here
Your first macro definition looks normal. You are using the macro to make the names of the input and output datasets dynamic.
Your second macro definition makes no sense and will not work.
Did you define a macro named OVER somewhere? There isn't any macro statement named %OVER.
Arrays are DATA STEP ELEMENTS and can therefore NOT BE USED AS MACRO ELEMENTS!
It is the same for all data step variables.
The macro preprocessor is not designed to handle data, it is designed to handle code.
@Srigyan wrote:
I have two codes
Code-1
%macro Zero (Input=, Output=); data &Output.; set &Input.; array change _numeric_; do over change; if change=. then change=0; end; run ; %mend;
Code-2
%macro Zero (Input=, Output=); data &Output.; set &Input.; array change _numeric_; %do %over change; %if change=. %then change=0; %end; run ; %mend;
the only difference is % sign with loop in code-2. I get the same output.
Question is:
1) Why and where % sign is require for macro.
2) if the output is same in both the query why do we need % sign for loop here
Did you actually read the log?
48 %macro Zero (Input=, Output=); 49 data &Output.; 50 set &Input.; 51 array change _numeric_; 52 %do %over change; ERROR: An unexpected semicolon occurred in the %DO statement. ERROR: A dummy macro will be compiled. 53 %if change=. %then change=0; 54 %end; 55 run ; 56 %mend; NOTE: The macro ZERO completed compilation with errors. 0 instructions 0 bytes.
And executing the macro:
57 %zero(input=junk, output=work.j) - 180 WARNING: Apparent invocation of macro ZERO not resolved.
I suspect that you also reused the same data set names and because the macro with the second definition did not execute that the original output data set was not replaced. So you only think it generated the same output.
Moral of the story:
1) Read the log
2) Different code should use different macro names so you know which you are actually testing
3) Generate different output data set names to actually compare the results.
There is a reason for "Read the Log" being second in my list of Maxims. It's THAT important.
READ THE LOG
READ THE LOG
READ THE LOG
data class;
set sashelp.class;
if _n_=1 then call missing(weight,height);
run;
proc stdize data=class out=want missing=0 reponly;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.