SAS macro output: equal sign as a macro argument

I am writing a macro that at some point calls some proc code. I want the user to be able to specify arbitrary proc sql parameters (for example, inobs = 100 may be one of the input arguments for my macro).

It is very difficult for me to refer to an argument that has an equal sign =.

One of the problems is that I must also check if the macro argument is empty or not, and if it is not empty, only then add the specified parameters to the sql statement.

Below is an example of a dough test that does not work and throws

ERROR: The INOBS keyword parameter was not set by the macro.

I read this ( http://www2.sas.com/proceedings/sugi28/011-28.pdf ) and other SUGIs and tried many ways to quote and invoke the macro.

If someone can provide a working example of the function below, we will be very grateful.

options mprint mlogic;

data have;
    length x $8;
    input x;
    datalines;
one
two
three
;

proc sql inobs=2;
    create table sql_output as 
            select * 
            from have;
quit;


%macro pass_parameter_with_equal_sign(table=, sqlOptions=);
    proc sql 
%if "%left(%trim(&sqlOptions.))" ne "" %then %do;
    &sqlOptions.
%end;
    /* the semicolon to end the proc sql statement */
    ;
        create table macro_output as 
            select * 
            from have;
    quit;
%mend;

%pass_parameter_with_equal_sign(table=have, sqlOptions=%str(inobs=2))

title "SQL output:";
proc print data=sql_output; run;
title "Macro output:";
proc print data=macro_output; run;
+4
source share
3 answers

Ah, that was a difficult problem that you encountered. In fact, the problem is caused by calls %trim()and %left().

Removing these results in code that works as intended (note, I also deleted the macro quoting the parameter):

%macro pass_parameter_with_equal_sign(table=, sqlOptions=);
    proc sql 
    %if "&sqlOptions" ne "" %then %do;
        &sqlOptions
    %end;
    /* the semicolon to end the proc sql statement */
    ;
        create table macro_output as 
            select * 
            from &table;
    quit;
%mend;

%pass_parameter_with_equal_sign(table=sashelp.class, sqlOptions= inobs=2);

We can recreate the problem you are facing:

%put %trim(inobs=1);

inobs = 1, %trim() , . , "inobs = 1", :

%let param = inobs=1;
%put %trim(%str(&param));

. Amir %if , . , .


1 - %left() %trim

, "%left(%trim(&sqlOptions.))". , ( ), . , :

%let param =      lots      of     spaces        ;
%put ***&param***;

:

***lots      of     spaces***

, , . , %str().

%let param = %str(     lots      of     spaces        );
%put ***&param***;

:

***     lots      of     spaces        ***

2 - ,

, , , %left() %trim(), , . :

%let param = %str(     inobs = 2        );

, % str(), . , , :

%put %trim(&param);  * ALREADY QUOTED AT CREATION SO THIS WORKS FINE;

, %left(), :

%put %left(%trim(&param));  * OOPS. DOESNT WORK;

, , , , , %trim() , . :

%put %unquote(%trim(&param));

% str():

%put %left(%str(%trim(&param)));

... % nrstr():

%let param = %str(     inobs = 2        );
%put %left(%trim(%nrstr(&param)));

... %sysfunc() datastep:

%put %sysfunc(compress(&param));
+1

%if , :

%macro pass_parameter_with_equal_sign(table=, sqlOptions=);
    proc sql 
    &sqlOptions.
    /* the semicolon to end the proc sql statement */
    ;
        create table macro_output as 
            select * 
            from have;
    quit;
%mend;

%if, , , &sqlOptions , , , , :

proc sql inobs=2; /* in the case of &sqlOptions=inobs=2 */

&sqlOptions , :

proc sql; /* i.e. no options specified */

.

, Amir.

+4

Amir, , . , , Chang Chung ?.

C8 , .

%if %sysevalf(%superq(param)=,boolean) %then ... /* C8 */ 

:

%macro test_me(param=);

  %if %sysevalf(%superq(param)=,boolean) %then %put Empty;
  %else %put Not Empty;;
%mend test_me;

%test_me(param=);
%test_me(param=MyParam);
%test_me(param=param=5);

% SUPERQ , . - - ; , .

C4 ( SUPERQ SYSEVALF) , , .

+3
source

Source: https://habr.com/ru/post/1617322/


All Articles