Samuel
Samuel

Reputation: 49

Fail to Pass Parameter to SAS Macro

Can you help me out on this situation? i have tried to pass several headers in a array to SAS macro to create graphs. I am pretty new to SAS programming and I can't figure out how it happened. Thanks a lot for your help.

/*Create Trend Chart*/
%macro trendChart(dataFile,xVariable,yVariable,group);
    proc gplot data=&dataFile;
        plot &yVariable*&xVariable=&group;
        symbol1 v=star c=blue;
        title "Time Series Plot";
    run;
    quit;
%mend trendChart;

data _null_;
    /*Create Trend Chart*/
    Array variableList[*] Res_Rd1 OW_perp_Rd2;
    do i=1 to dim(variableList);
        var_name=vname(variableList[i]);
        put var_name;
        %trendChart(TDMR.Children,M_Date,var_name,t_);
    end;    
run;

enter image description here

Upvotes: 0

Views: 223

Answers (1)

Tom
Tom

Reputation: 51621

You cannot reference the values of data step variables in macro calls. So in your macro call the value VAR_NAME is just passed to the macro as the name of variable to use in the PROC that it is generating.

You cannot nest steps within each other. When the macro call executes it generates SAS code that is then compiled and executed by SAS. When SAS sees a PROC or DATA step starting it immediately stops compiling the current step. So your data step will have an unclosed DO loop when SAS sees the PROC statement generated by the macro call.

You could use a new macro to generate multiple calls to your existing macro.

%macro run_charts(varlist);
 %local i ;
 %do i=1 %to %sysfunc(countw(&varlist));
    %trendChart(TDMR.Children,M_Date,%scan(&varlist,&i),t_);
 %end;
%mend run_charts;
%run_charts(Res_Rd1 OW_perp_Rd2);

If you want to use a DATA step instead then use CALL EXECUTE() to push the macro calls onto the stack to run after the data step ends. In a data step if you want to loop over a list of string constant values you can list then in the DO statement. No need to make an array of dummy variable names.

data _null_;
  length var_name $32 ;
  do var_name='Res_Rd1','OW_perp_Rd2';
    call execute('%nrstr(%trendChart)(TDMR.Children,M_Date,'
         ||var_name ||',t_);'
    );
  end;
run;

Upvotes: 1

Related Questions