DATA Step, Macro, Functions and more

Macro statment in Proc SQL

Accepted Solution Solved
Reply
Super Contributor
Posts: 271
Accepted Solution

Macro statment in Proc SQL

[ Edited ]
%macro m(text);
  proc sql noprint;
   create table t as
     select *
	   from sashelp.class
	 %if &text ^=%str() %then where name="William"; %str(;)
   quit;
%mend m;
%m(tx)

I have a SAS program above. When I remove %str( ; ), it does not work.


Accepted Solutions
Solution
‎05-06-2016 03:54 PM
Trusted Advisor
Posts: 1,117

Re: Macro statment in Proc SQL

Posted in reply to SAS_inquisitive

Hello @SAS_inquisitive,

 

I think you can simplify the line "%if ..." to

%if &text ^= %then where name="William";;

The first semicolon ends the %THEN statement, the second semicolon ends the SELECT statement.

View solution in original post


All Replies
Solution
‎05-06-2016 03:54 PM
Trusted Advisor
Posts: 1,117

Re: Macro statment in Proc SQL

Posted in reply to SAS_inquisitive

Hello @SAS_inquisitive,

 

I think you can simplify the line "%if ..." to

%if &text ^= %then where name="William";;

The first semicolon ends the %THEN statement, the second semicolon ends the SELECT statement.

Super User
Posts: 11,343

Re: Macro statment in Proc SQL

[ Edited ]
Posted in reply to SAS_inquisitive

You should specify "how it doesn't work".

 

 

If you run the not working code with Options mprint;

 

I think you will see that there is no semi-colon in the code before the Quit. So Quit is encountered in an expected place:

 

...

   from sashelp.class

quit;

 

or

...

   from sashelp.class

      where name="William"

 quit;

Super Contributor
Posts: 271

Re: Macro statment in Proc SQL

@FreelanceReinhardand @ballardw.  Thank you. I also wonder the progam flow of this code.  Does the %IF statment executes first to generate the  SAS statements before Proc Sql statement? 

Trusted Advisor
Posts: 1,117

Re: Macro statment in Proc SQL

Posted in reply to SAS_inquisitive

SAS_inquisitive wrote:

Does the %IF statment executes first to generate the  SAS statements before Proc Sql statement? 


Yes, sure. That's how SAS macro language works. Roughly speaking, the macro processor generates the code (data steps, proc steps, other non-macro statements) that is eventually sent to the compiler. Please see the documentation How SAS Processes Statements with Macro Activity for a detailed overview.

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 250 views
  • 2 likes
  • 3 in conversation