Or this:
%Macro testing(I);
data sasdata.newlist&i;
set sasdata.oldlist;
if &&restri&i; /* simple subsetting if in data step, and condition replaced */
run;
%mend testing;
Never try to handle actual data with macro statements (that's what you did with using the macro %if instead of the data step if).
The subsetting if may be needed in some instances; in this simple example, the where condition as suggested by @ChrisNZ will be sufficient and perform better.
Like this?
%let restri2=sex='F';
%macro testing(I);
data T;
set SASHELP.CLASS;
where &&restri&i ;
run;
%mend testing;
%testing(2)
NOTE: There were 9 observations read from the data set SASHELP.CLASS.
WHERE sex='F';
Or this:
%Macro testing(I);
data sasdata.newlist&i;
set sasdata.oldlist;
if &&restri&i; /* simple subsetting if in data step, and condition replaced */
run;
%mend testing;
Never try to handle actual data with macro statements (that's what you did with using the macro %if instead of the data step if).
The subsetting if may be needed in some instances; in this simple example, the where condition as suggested by @ChrisNZ will be sufficient and perform better.
While it's hard to tell when to use macro statement, when not.
for example: Do I need to put % in the if -then-else statement and do while statement? By the way, can I use "Do i = 1 to n while (condition)" statement here like this?
%MACRO FUNDSOURCE(I);
DATA SASDATA.STUDENT&I;
SET SASDATA.STUDENTLIST
DO M = 1 TO 310 WHILE(&&BUDG&I > 0); /*loop through all observations_ALL STUDENTS*/
IF &&BUDG&I LE 3000- FA_TOT1 THEN do;
DISBURSE = &&BUDG&I;
FA_TOT1+DISBURE;
&&BUDG&I - DISBURSE;
end;
ELSE IF &&BUDG&I GT (3000- FA_TOT1) THEN DO;
DISBURSE = 3000-FA_TOT1;
FA_TOT1+DISBURSE;
&&BUDG&I - DISBURSE;
END;
END;
IF _n_ > M THEN DELETE; /*if budget are all gone, delete other observations, keep observations only for the student who get funds*/
RUN;
%MEND FUNDSOURCE;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.