BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sharathr
Obsidian | Level 7
Hi forum,

Is it possible to assign SAS statements in macro variable refer example below.
%let condition= where a>5;
%macro test;
Data test;
Set source;
& Condition.;
Run;
%macro test;
%test;
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

As you noticed, you can do this.  However, notice a couple of features about your program.

 

The value of &CONDITION does not include a semicolon ending the WHERE statement.  The semicolon at the end of the %LET statement is needed to end the %LET statement.  So the value of &CONDITION is:

 

where a>5

 

The program that refers to &CONDITION uses:

 

&condition;

 

That added semicolon following &CONDITION is what ends the WHERE statement.

 

If you wanted to include a semicolon as part of the value of &CONDITION, that is possible using:

 

%let condition = %str(where a>5; );

 

The %STR function treats the first semicolon as text, not as a symbolic character that ends the %LET statement.

View solution in original post

5 REPLIES 5
andreas_lds
Jade | Level 19

You can answer your question by executing the code, reading the log and fixing errors that may be shown. If you need help fixing errors, post the log in code window see {i}-icon above the editing window.

Sharathr
Obsidian | Level 7
The code is working thanks.
Astounding
PROC Star

As you noticed, you can do this.  However, notice a couple of features about your program.

 

The value of &CONDITION does not include a semicolon ending the WHERE statement.  The semicolon at the end of the %LET statement is needed to end the %LET statement.  So the value of &CONDITION is:

 

where a>5

 

The program that refers to &CONDITION uses:

 

&condition;

 

That added semicolon following &CONDITION is what ends the WHERE statement.

 

If you wanted to include a semicolon as part of the value of &CONDITION, that is possible using:

 

%let condition = %str(where a>5; );

 

The %STR function treats the first semicolon as text, not as a symbolic character that ends the %LET statement.

Kurt_Bremser
Super User

I would NOT do it that way; if I have to do something similar, I do this:

%let condition=a>5;

data test;
set source;
where &condition.;
run;

That way it is clear which statement is executed, and only the condition itself comes from the macro variable. This makes code much more read- and maintainable.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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