how to use &&var&i in if condition

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

how to use &&var&i in if condition

/* Create macro variables to store restrictions */
PROC SQL NOPRINT;
SELECT RESTRICTIONS
 INTO :RESTRI1 - :RESTRI35 FROM SASDATA.restriction; 
QUIT;
%PUT &RESTRI2;
 
/*the resolved value is: */
gender = 'M' and state = 'CA'
 
/*I want to create a data set sasdata.newlist&i when the ith restriction
 is &&restri&i (eg: gender = 'M' and state = 'CA')*.
***While the sasdata.newlist2 contains all data in sasdata.oldlist, the if condition doesn't work. Anybody can help me to solve this problem?*/***
 
%Macro testing(I);
     data sasdata.newlist&i;
     set sasdata.oldlist;
     %if &&restri&i %then; /*I only want the observations which meet the restriction &&restri&I* in this new created dataset/
     run;
%mend testing;
%testing(2)

Accepted Solutions
Solution
‎05-30-2018 06:27 PM
Super User
Posts: 10,284

Re: how to use &&var&i in if condition

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
PROC Star
Posts: 2,375

Re: how to use &&var&i in if condition

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';

 

 

Solution
‎05-30-2018 06:27 PM
Super User
Posts: 10,284

Re: how to use &&var&i in if condition

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 5

Re: how to use &&var&i in if condition

Posted in reply to KurtBremser

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;

Occasional Contributor
Posts: 5

Re: how to use &&var&i in if condition

Thanks, it works
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 148 views
  • 0 likes
  • 3 in conversation