Search code examples
sassas-macro

Using max function in macro SAS


I'm trying to find the max of four variables, Value_A Value_B Value_C Value_D, within a macro. I thought I could do %sysfunc(max(value_&i.)) but that isn't working. My full code is:

%let i = (A B C D);
%macro maxvalue;

    data want;
    set have;
        %do j = 1 %to %sysfunc(countw(&list.));
        %let i = %scan(&list.,&j.);
            value_&i.= Sale_&i. - int_&i.
            Max_Value = %sysfunc(max(value_&i.));
        %end;
    run;
%mend maxvalue;
%maxvalue;

I should specify that I only want the max of the four variables for each observation. Thanks for your help!


Solution

  • As aforementioned, you're over-complicating this, but you can achieve what you're trying to do using macro logic by including another for loop within your max_value assignment. This method involves you taking the max of your four variables and a missing value, which should produce the desired result:

    %let list = A B C D;
    
    %macro maxvalue;
    
        data want;
            set have;
                %do j = 1 %to %sysfunc(countw(&list.));
                %let i = %scan(&list.,&j.);
                    value_&i.= Sale_&i. - int_&i.
                %end;
    
                max_value = max(
                    %do x = 1 %to %sysfunc(countw(&list.));
                    %let y = %scan(&list.,&x.);
                        value_&y.,
                    %end; .
                );
        run;
    
    %mend maxvalue;
    
    %maxvalue;