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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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