0
votes

The following DATA Step is part of a SAS macro. There are two conditions that should be checked when reading the data set return_check. If the first or second conditions are met, then a macro variable "Data_status" should take the string value 'Exclude'. If neither conditions are met then the macro variable "Data_status" should take the string value 'Include'. When I run the DATA Step the first condition is met. This is evident as the variable "Status" in the data set check2 has the string value "Exclude". However, and here come what is puzzling me, after the DATA Step, the macro variable Data_status resolves to "Include". I am not sure what is it that I am doing wrong. Any help will be highly appreciated.

%macro analysis;
----------codes not shown--------
proc means data=temp noprint; *A prior step that produces the input dataset return_check;
    var ret;
    by event_id ab_:;
    output out=return_check nmiss=missing_ret;
run;

data check2;
    set return_check;
    if Ab_M2=Ab_m1=ab_0=Ab_1=Ab_2=0 and missing_ret>(_FREQ_-50) then do; *Condition 1;
        call symput('Data_status','Exclude');
        Status=symget("Data_status");
    end;
    else if ab_M1=1 and ab_0=1 and ab_1=1 and missing_ret=1 then do; *Condition 2;
        call symput('Data_status','Exclude');
        Status=symget("Data_status");
        end;
    else do; 
        call symput('Data_status','Include');
        Status=symget("Data_status");
    end; 
run;
%put &Data_status;
%if &Data_status eq %bquote(Exclude) %then %do;
%mend analysis;
----------codes not shown--------

Here is a copy of the input Data set "return_check"

Ab_M2 Ab_M1 Ab_0 Ab_1 Ab_2 _TYPE_ _FREQ_ missing_ret 
0 0 0 0 0 0 100 100 
0 0 0 0 1 0 1 1 
0 0 0 1 0 0 1 1 
0 0 1 0 0 0 1 1 
0 1 0 0 0 0 1 1 
1 0 0 0 0 0 1 1 

Thanks for your time in advance

2
Please provide a sample dataset that will allow people to reproduce the issue.user667489
Your program is only setting the macro variable based on the variable values in the last observation of the input dataset.Tom
Thanks for your suggestion user667489. I added the input data set.DR_M
Tom, it is not my intention to do as what you mentioned. Is there a problem in the way I structured the conditions?DR_M
What is the intended behaviour for the macro variable at the end of your data step? E.g. do you want it to be set to 'exclude' if the status of any of the individual rows is 'exclude'?user667489

2 Answers

0
votes

To overcome the problem I faced in the initial program, I added a stop statement to stop the DATA Step from processing when any of the conditions is met, to pass the appropriate value to the macro variable.

data check2;
        set return_check;
        if Ab_M2=Ab_m1=ab_0=Ab_1=Ab_2=0 and missing_ret>(_FREQ_-50) then do; *Condition 1;
            call symput('Data_status','Exclude');
            Status=symget("Data_status");
            stop;
        end;
        else if ab_M1=1 and ab_0=1 and ab_1=1 and missing_ret=1 then do; *Condition 2;
            call symput('Data_status','Exclude');
            Status=symget("Data_status");
            stop;
            end;
        else call symput('Data_status','Include'); 
    run;
%put &Data_status;
0
votes

A macro variable can only have one value at any point in time. For your sample dataset only the first observation will meet the conditions for Status='Exclude'.

Ab_M2=0 Ab_M1=0 Ab_0=0 Ab_1=0 Ab_2=0 _TYPE_=0 _FREQ_=100 missing_ret=100 Status=Exclude
Ab_M2=0 Ab_M1=0 Ab_0=0 Ab_1=0 Ab_2=1 _TYPE_=0 _FREQ_=1 missing_ret=1 Status=Include
Ab_M2=0 Ab_M1=0 Ab_0=0 Ab_1=1 Ab_2=0 _TYPE_=0 _FREQ_=1 missing_ret=1 Status=Include
Ab_M2=0 Ab_M1=0 Ab_0=1 Ab_1=0 Ab_2=0 _TYPE_=0 _FREQ_=1 missing_ret=1 Status=Include
Ab_M2=0 Ab_M1=1 Ab_0=0 Ab_1=0 Ab_2=0 _TYPE_=0 _FREQ_=1 missing_ret=1 Status=Include
Ab_M2=1 Ab_M1=0 Ab_0=0 Ab_1=0 Ab_2=0 _TYPE_=0 _FREQ_=1 missing_ret=1 Status=Include 

If you just want it to stop when it finds a record that meets the condition then you can simplify your code.

%let data_status=Include;
data _null_;
  set return_check;
  if (Ab_M2=Ab_m1=ab_0=Ab_1=Ab_2=0 and missing_ret>(_FREQ_-50) )
   or (ab_M1=1 and ab_0=1 and ab_1=1 and missing_ret=1 ) then do;
    call symputx('Data_status','Exclude');
    stop;
  end;
run;