Discover More Tips and Techniques on This Blog

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:

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

    ReplyDelete
  2. 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;

    ReplyDelete

Disclosure:

In the spirit of transparency and innovation, I want to share that some of the content on this blog is generated with the assistance of ChatGPT, an AI language model developed by OpenAI. While I use this tool to help brainstorm ideas and draft content, every post is carefully reviewed, edited, and personalized by me to ensure it aligns with my voice, values, and the needs of my readers. My goal is to provide you with accurate, valuable, and engaging content, and I believe that using AI as a creative aid helps achieve that. If you have any questions or feedback about this approach, feel free to reach out. Your trust and satisfaction are my top priorities.