Wolfspirit
Wolfspirit

Reputation: 155

SAS If conditional before PROC SORT

if &var. not in ('quarter' 'period' 'year') then do;
    proc sort data=test_&var.;
        by descending column1;
    run;
end;

I'm trying to attach a conditional statement before a proc sort statement, but I'm having trouble getting the IF to work outside of a data step. Is there some way around this?

Basically I don't want to sort any test_&var. datasets that contain a time variable as those are the only datasets I want to retain a particular sequence in.

Upvotes: 0

Views: 3518

Answers (1)

Allan Bowe
Allan Bowe

Reputation: 12691

Here is a macro example:

/* tell macro language to recognise the in operator */
options minoperator;

/* demo data */
data test_yymm; column1=1;run;
data test_quarter; column1=1;run;

%macro demo(var) ;
/* remove quotes, place not outside the condition */
%if not (&var in quarter period year) %then %do;
    proc sort data=test_&var.;
        by descending column1;
    run;
%end;
%mend;

/* call macro */
%demo(yymm);
%demo(quarter);

You could also achieve this in a data step, by calling call execute:

%let var=yymm;
data _null_;
  if "&var" not in ('quarter' 'period' 'year') then do;
    call execute("
      proc sort data=test_&var.;
        by descending column1;
      run;
    ");
  end;
run;

Remember that SAS macro language is text based, so variables do not need to be quoted. Unlike regular SAS code, in which string literals DO need to be quoted in order to be recognised as such..

Upvotes: 3

Related Questions