BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Chaichai
Fluorite | Level 6
/* 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)
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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.

View solution in original post

4 REPLIES 4
ChrisNZ
Tourmaline | Level 20

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

 

 

Kurt_Bremser
Super User

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.

Chaichai
Fluorite | Level 6

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;

Chaichai
Fluorite | Level 6
Thanks, it works

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 631 views
  • 0 likes
  • 3 in conversation