DATA Step, Macro, Functions and more

Assigning SAS statements to macro variable

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Assigning SAS statements to macro variable

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;

Accepted Solutions
Solution
‎01-18-2018 03:40 AM
Super User
Posts: 6,632

Re: Assigning SAS statements to macro variable

[ Edited ]

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


All Replies
Valued Guide
Posts: 519

Re: Assigning SAS statements to macro variable

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.

Occasional Contributor
Posts: 12

Re: Assigning SAS statements to macro variable

The code is working thanks.
Solution
‎01-18-2018 03:40 AM
Super User
Posts: 6,632

Re: Assigning SAS statements to macro variable

[ Edited ]

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.

Occasional Contributor
Posts: 12

Re: Assigning SAS statements to macro variable

Thanks
Super User
Posts: 9,890

Re: Assigning SAS statements to macro variable

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 133 views
  • 0 likes
  • 4 in conversation