Search code examples
sassas-macro

Why is this macro contrasting Day_i with Day_(1-1) not working?


Why is this macro not working?

%MACRO Cerved_recap;

%let i=2;
%DO i = 2 %TO &oggi.;

%let ieri = %sysevalf(&i.-1);

data work.initial_db_cerved;
set work.initial_db_cerved;
length esito_&i. $ 10;
format esito_&i. $char20.;

%if ENCOURS_TOTAL_&i. < ENCOURS_TOTAL_&ieri. %then %do; esito_&i.='total_out';
%end;
%else %do; esito_&i.= '-';
%end;

run;

%end;
%MEND Cerved_recap;
%Cerved_recap;

Solution

  • Here is a technical answer, to get a real answer explain in words what you are trying to do, and perhaps show an example of the working SAS code you are trying to use the macro to generate.

    Since you set I to a digit string like 2 and set IERI to a digit string like 1 the test in your %IF statement will usually be false. It is comparing a string like ENCOURS_TOTAL_2 to another string like ENCOURS_TOTAL_1 and since the beginning of those two strings are exactly the same it just like comparing the digit 2 to the digit 1. The test will only be true when I has a value that is a power of 10 (10 100 1000 etc) because then you are comparing a string that starts with the digit 1 to a string that starts with the digit 9.

    Perhaps you wanted to compare the values of SAS variables with those names?

    If so then use actual SAS statements instead of macro statements.

    if ENCOURS_TOTAL_&i. < ENCOURS_TOTAL_&ieri. then esito_&i.='total_out';
    else esito_&i.= '-';