Search code examples
sassyntax-errorsas-macro

Average values in two consecutive values


I have the following code in a macro: ...

data HE2update (drop=sum_values n_values);
    set dst_end_update;
    by short_date HE;

    %if HE=2 or 3 %then %do;
        sum_values+value;
        n_values+1;

        %if HE=2 %then delete;
            %else %if HE=3 %then %do;
            value = round(sum_values/n_values);
            HE=2;
            %end;
    %end;
    %if HE>3 %then HE=HE-1;
        %else HE;
run;

I am getting the following Error: ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *, **, +, -, /, <, <=, <>, =, >, ><, >=, AND, EQ, GE, GT, IN, LE, LT, MAX, MIN, NE, NG, NL, NOTIN, OR, ^=, |, ||, ~=.

The error seems to be with the last if/then/else statement I don't see what I need to correct. Any help would be greatly appreciated.


Solution

  • I don't think you need macro logic here at all.

    data HE2update (drop=sum_values n_values);
        set dst_end_update;
        by short_date HE;
    
        if HE in (2, 3) then do;
            sum_values+value;
            n_values+1;
    
            if HE=2 then delete;
                else if HE=3 then do;
                value = round(sum_values/n_values);
                HE=2;
                end;
        end;
        if HE>3 then HE=HE-1;
    
    run;