Search code examples
sassas-macro

How can I add id-s for variables with a specific conditions?


In SAS I'd like to add id values to the variables with a specific conditions. I have the following code:

DATA market_new;
SET sashelp.cars;
if Make = 'Audi' then id = 0;
else id = _N_;  
RUN;

proc print data=market_new;
run;

Output:

enter image description here

The problem is that the id continues with 27, 28 etc. after the make isn't equal to Audi. My goal is to have 8, 9 instead.


Solution

  • Use a SUM (+) statement to track the Audis.

    if make='Audi' then do;
      audi_seq + 1;        drop audi_seq;
      audi_id = audi_seq;
    end;
    else
      audi_id = 0;