NicChik
NicChik

Reputation: 73

macro variable won't resolve SAS

When I run macro "quantplot" I run into the issue shown in the picture:

Picture of Error.

Essentially what appears to be happening is that none of the expressions in this form recode_&variables, out_&variables, etc. seems to be returning the correct value. It's simply reading it as recode_ and ignoring the macro variable.

I have provided the following code for sample data:

data test (drop=i);
do i=1 to 1000;
a=round(uniform(1)*4,.01);
b=round(uniform(1)*10,.01);
c=round(uniform(1)*7.5,.01);
u = rand("Uniform");
y=floor( (2)*u );
output;
end;
stop;
run;

%macro percentiles(indep_var=,num_groups=);
%let recodes=recode_%sysfunc(tranwrd(&indep_var,%str( ),%str( recode_)));
proc rank data=test out=test groups=7;
var &indep_var;
ranks &recodes;
run;
%mend;  
%percentiles(indep_var=a b c);

And also the code for the macro that is currently not working. Please help!

/*Plots to determine  functional form for quantitative variables*/
%macro quantplot(indep_var=,dep_var=);
    /* Count the number of words in the input */                                                                                                                                   
    %let count=%sysfunc(countw(&indep_var)); 
    /* Loop through the total number of values */                                                                                         
    %do i = 1 %to &count;                                                                                                              
        %let variables=%qscan(&indep_var,&i,%str( ));
        %put(&variables); 
        proc means data=test;
        %put(&variables); 
            class recode_&variables;
            var &dep_var;
            output out = out_&variables mean = pby_&variables;
        run;
        data p_&variables;
            set out_&variables; if _type_=1;
            lnp = log(pby_&variables/(1-pby_&variables));
        run;
        ods graphics on;
            proc gplot data = p_&variables;
                symbol value = star i=join;
                plot lnp*recode_&variables;
                plot pby_&variables*recode_&variables;
            run;
        ods graphics off;
    %end;
%mend;
%quantplot(indep_var=a b c,dep_var=y);

Upvotes: 0

Views: 237

Answers (1)

Quentin
Quentin

Reputation: 6378

The problem is that you have introduced macro quoting by using %qscan:

%let variables=%qscan(&indep_var,&i,%str( ));

and sometimes this quoting is not automatically removed when it should be, and it breaks the tokenization of:

class recode_&variables;

Whenever MPRINT shows code that looks correct, but it errors, you should suspect a macro quoting problem. You can either unquote the value yourself:

class %unquote(recode_&variables);

or change your use of %qscan to %scan.

Your solution of using %sysfunc(strip()) works because %sysfunc() unquotes the value.

Upvotes: 5

Related Questions