Friday, January 2, 2009

macro for sorting the the datasets

MACRO FOR SORTING:

Rather than using the Proc Sort procedure all the time..... you can just use the following macro....

and call it when you req... to sort any SAS dataset.....

EXAMPLE1:
%macro srt(dtn,keyvar);
proc sort data=&dtn;
by &keyvar;
run;
%mend srt;


%srt(ie,PT IEORRES);

*the above step will tell SAS to sort the IE dataset with the by variables PT and IEORRES respectively.


EXAMPLE2:
*Sometimes we need to create an output dataset during the sorting process i.e in the Proc sort step in such a case use the below macro to do the same;

%MACRO SORT(IN=,VAR=,OUT=);
PROC SORT DATA=&IN OUT=&OUT;
by &VAR;
RUN;
%MEND SORT;

%SORT(IN=CEC1,VAR=PT,OUT=CEC2);
%SORT(IN=DERIVE.HEADER,VAR=PT,OUT=HEADER1);


EXAMPLE3:

*Sometimes we need to use the NODUPKEY option to delete the duplicate observations in the dataset in such a case use the below macro to do the same;

%MACRO SORT(IN,VAR,OUT,OPTN);

PROC SORT DATA=&IN OUT=&OUT &OPTN ;

by &VAR;

RUN;

%MEND SORT;

%SORT(IN=AE, AE1,USIBJID AEBODYSYS AESEV, NODUPKEY);







2 comments:

Anonymous said...

how to pass n number of datasets for sorting in sas using macro?

sarath said...

proc contents data=work._all_ out=contents;
run;

proc sql;
create table list as select distinct memname from contents;
quit;

*pass n number of datasets from work library ;
data _null_;
set list;
call execute ('%macro('||left(trim(memname))||')');
run;

%macro is the macro you want to run for all the datasets;

Post a Comment

ShareThis