Search code examples
if-statementsas-macro

Count and generate a constant in SAS


For each id count the number of qc=fails, generate a new column final with all values as "repeat" if any one id has 2 or more than 2 fails, else pass. Repeat if count is greater than equal to 2. For some reason the if-else statement does not work. Instead of the final being a repeat for count greater than equal to 2, it is displaying it as pass.dataset

%let repeat_flag = 0;
%let pass_flag = 0;
data _null_;
  set exp;
  if count ge 2 then call symputx ('repeat_flag',1);
    else call symputx ('pass_flag',1);
  stop;
run;
%if &repeat_flag %then %do;
  data exp;
    set exp;
        Final = 'REPEAT';
  run;
%end;
%if &pass_flag %then %do;
  data exp;
    set exp;
        Final = 'PASS';
  run;
%end;

%put &repeat_flag; %put &pass_flag;


Solution

  • If you want to test if any observation has COUNT larger than one then use a pattern like this instead. Set the macro variable to the FALSE condition and then set it TRUE when the condition is met.

    %let pass_flag=PASS;
    data _null_;
      set exp;
      if count ge 2 then do;
         call symputx ('pass_flag','REPEAT');
         stop;
      end;
    run;
    data want;
      set exp;
      length final $6;
      final="&pass_flag" ;
    run;